gpt4 book ai didi

ruby-on-rails - 简单的 redirect_to 显示路径不起作用

转载 作者:行者123 更新时间:2023-12-04 23:58:50 27 4
gpt4 key购买 nike

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

路线.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"}

但是加载的页面是新页面。为什么没有加载显示操作/ View ?

最佳答案

这是因为 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"}

当从顶部开始检查路由时,第一个 Action 被调用

关于ruby-on-rails - 简单的 redirect_to 显示路径不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6339725/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com