简单的redirect_to显示路径不起作用

ben*_*ben 2 routing ruby-on-rails ruby-on-rails-3

我试图在创建资源后显示它.

的routes.rb

  resources :eco_systems do
    member do
      get 'new'
      post 'create'
      get 'show'
    end
  end
Run Code Online (Sandbox Code Playgroud)

eco_systems_controller.rb

class EcoSystemsController < ApplicationController
  def new
    @eco_system = EcoSystem.new
  end
  def create
    @eco_system = current_user.eco_systems.create(params[:eco_system])
    redirect_to eco_system_path(@eco_system.id)
  end
  def show

  end
end
Run Code Online (Sandbox Code Playgroud)

redirect_to eco_system_path(@eco_system.id)运行时,产生的URL是

http://localhost:3000/eco_systems/5
Run Code Online (Sandbox Code Playgroud)

控制台输出:

Started GET "/eco_systems/5" for 127.0.0.1 at 2011-06-14 16:04:22 +1000
  Processing by EcoSystemsController#new as HTML
  Parameters: {"id"=>"5"}
Run Code Online (Sandbox Code Playgroud)

但加载的页面是新页面.为什么不加载show动作/视图?

Boh*_*dan 8

之所以会发生这种情况,show是因为new如果你运行rake routes你会看到动作

 eco_system GET    /eco_systems/:id(.:format)      {:action=>"new", :controller=>"eco_systems"}
            POST   /eco_systems/:id(.:format)      {:action=>"create", :controller=>"eco_systems"}
            GET    /eco_systems/:id(.:format)      {:action=>"show", :controller=>"eco_systems"}
Run Code Online (Sandbox Code Playgroud)

当从顶部检查路由时,调用第一个动作