简单的redirect_以显示路径不起作用

问题描述

| 创建后,我试图显示资源。 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)
时,结果URL为
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\"}
但是加载的页面是新页面。为什么不加载显示动作/视图?     

解决方法

        之所以会这样,是因为如果您运行
rake routes
,则在
new
之后定义了
show
动作,您会看到
 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\"}
从顶部开始检查路线时,将调用第一个操作