gpt4 book ai didi

ruby-on-rails-3 - Rails _path 帮助程序生成格式不是 id 的路径

转载 作者:行者123 更新时间:2023-12-03 00:41:21 26 4
gpt4 key购买 nike

在我的routes.rb中我有:

  resources :aquariums do
resources :management_roles
resources :graphs
resources :animals
end
get 'aquarium', to: 'aquariums#show', :as => :aquarium

最后一次获取的原因是我的应用程序中有“当前水族馆”的概念。比如说,如果 current_aquarium 设置为 1,那么在我的 Controller 的“show”操作中,“/aquarium”将获得与“/aquariums/1”相同的渲染,代码如下

@aquarium_id = params[:id] || current_aquarium.id

现在,我假设这是由于这种不同的路由,这段代码:

<%= link_to aquarium.name, aquarium %>

<%= link_to aquarium.name, aquarium_path(aquarium) %>

生成这样的路径:

/aquarium.1

我通常期望的地方:

/aquariums/1

对吗?

根据请求...这就是 rake routes | grep aquar产量:(我假设这是最后一条路线把事情搞砸了,但我本以为它会按顺序处理这些。而且,仅供引用,我最初将该路线放在顶部。将其移动到底部假设它会修复)。

    aquarium_management_roles GET    /aquariums/:aquarium_id/management_roles(.:format)          management_roles#index
POST /aquariums/:aquarium_id/management_roles(.:format) management_roles#create
new_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/new(.:format) management_roles#new
edit_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id/edit(.:format) management_roles#edit
aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#show
PUT /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#update
DELETE /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#destroy
aquarium_graphs GET /aquariums/:aquarium_id/graphs(.:format) graphs#index
POST /aquariums/:aquarium_id/graphs(.:format) graphs#create
new_aquarium_graph GET /aquariums/:aquarium_id/graphs/new(.:format) graphs#new
edit_aquarium_graph GET /aquariums/:aquarium_id/graphs/:id/edit(.:format) graphs#edit
aquarium_graph GET /aquariums/:aquarium_id/graphs/:id(.:format) graphs#show
PUT /aquariums/:aquarium_id/graphs/:id(.:format) graphs#update
DELETE /aquariums/:aquarium_id/graphs/:id(.:format) graphs#destroy
aquarium_animals GET /aquariums/:aquarium_id/animals(.:format) animals#index
POST /aquariums/:aquarium_id/animals(.:format) animals#create
new_aquarium_animal GET /aquariums/:aquarium_id/animals/new(.:format) animals#new
edit_aquarium_animal GET /aquariums/:aquarium_id/animals/:id/edit(.:format) animals#edit
aquarium_animal GET /aquariums/:aquarium_id/animals/:id(.:format) animals#show
PUT /aquariums/:aquarium_id/animals/:id(.:format) animals#update
DELETE /aquariums/:aquarium_id/animals/:id(.:format) animals#destroy
aquariums GET /aquariums(.:format) aquariums#index
POST /aquariums(.:format) aquariums#create
new_aquarium GET /aquariums/new(.:format) aquariums#new
edit_aquarium GET /aquariums/:id/edit(.:format) aquariums#edit
aquarium GET /aquariums/:id(.:format) aquariums#show
PUT /aquariums/:id(.:format) aquariums#update
DELETE /aquariums/:id(.:format) aquariums#destroy
aquarium GET /aquarium(.:format) aquariums#show

提前致谢!

格雷格

最佳答案

更改最后一条路线:

get 'aquarium', to: 'aquariums#show', :as => :aquarium

对此:

get 'aquarium', to: 'aquariums#show', :as => :current_aquarium

问题是你必须以相同的名称命名路由:

aquarium GET    /aquariums/:id(.:format)    aquariums#show
aquarium GET /aquarium(.:format) aquariums#show

如果您进行上述更改,那么当您将这些链接链接到调用时,第二条路线将不匹配...就目前而言,第二条路线是匹配的,就像路线所说的那样,它使用您的参数作为 :format。

如果您确实进行了此更改,并且有意链接到“current_aquarium”,则可能需要调整一些内容。

关于ruby-on-rails-3 - Rails _path 帮助程序生成格式不是 id 的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14172729/

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