Rails _path 帮助程序生成格式不是 id 的路径 [英] Rails _path helper generating path with format not id

查看:28
本文介绍了Rails _path 帮助程序生成格式不是 id 的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 routes.rb 中,我有:

In my routes.rb I have:

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

最后一次获取的原因是我的应用程序中有当前水族馆"的概念.例如,如果 current_aquarium 设置为 1,那么在我的控制器的显示"操作中,/aquarium"将获得与/aquariums/1"相同的渲染,代码如下

The reason for the last get is I have the notion of "current aquarium" in my app. If, say, current_aquarium is set to 1, then in my controller's 'show' action '/aquarium' gets the same rendering as '/aquariums/1' with code like

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

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

Now, and I'm assuming this is thanks to this different routing, this code:

<%= link_to aquarium.name, aquarium %>

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

生成这样的路径:

/aquarium.1

我通常期望的地方:

/aquariums/1

对吗?

每个请求...这里是什么 rake 路由 |grep aquar 产生:(我假设是最后一条路线把事情搞砸了,但我原以为它会按顺序处理这些.而且,仅供参考,我最初在顶部有这条路线.假设它移到底部会修复).

Per request... here's what rake routes | grep aquar yields: (I'm assuming it's that very last route that is messing things up, but I would have thought that it would process these in order. And, just FYI, I originally had that route at the top. Moved it to the bottom assuming it would fix).

    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

提前致谢!

格雷格

推荐答案

从这里更改最后一条路线:

Change that last route from this:

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

到此:

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

问题是你必须路由命名相同的东西:

The problem is that you have to routes named the same thing:

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

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

If you make the change above then that second route will not match when you make those link to calls... as it stands now, the second one is matching and like the route says is using your argument as the :format.

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

If you do make this change, you may need to tweak some things if you are intentionally linking to 'current_aquarium'.

这篇关于Rails _path 帮助程序生成格式不是 id 的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆