Rails _path helper生成格式为非id的路径



在我的路由。我有:

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

最后一个get的原因是我在我的应用程序中有"当前水族馆"的概念。如果,说,current_aquarium被设置为1,那么在我的控制器的"show"操作中,"/aquarium"获得与"/aquarium/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

如果你做了上面的改变,那么第二个路由将不匹配当你做那些链接到调用…现在,第二个是匹配的,就像路由说的那样,它正在使用你的参数作为:格式。

如果你做了这个改变,你可能需要调整一些东西,如果你故意链接到'current_aquarium'

相关内容

  • 没有找到相关文章

最新更新