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

时间:2011-06-14 06:11:34

标签: ruby-on-rails ruby-on-rails-3 routing

我正在尝试在创建资源后显示它。

的routes.rb

  resources :eco_systems do
    member do
      get 'new'
      post 'create'
      get 'show'
    end
  end

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

运行redirect_to eco_system_path(@eco_system.id)时,生成的网址为

http://localhost:3000/eco_systems/5

控制台输出:

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"}

但是加载的页面是新页面。为什么不加载show action / view?

1 个答案:

答案 0 :(得分: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"}

从顶部检查路线时,第一个动作被调用