Rails 3.1 has_one 嵌套资源:路由不生成“所有”资源路径
我有一个 has_one 关系:
# supplier.rb
has_one :presentation
...
# presentation.rb
belongs_to :supplier
...
以及它们的以下嵌套路由:
# routes.rb
resources :suppliers do
resource :presentation
end
运行rake paths
给出:
supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
GET ... {:action=>"show", :controller=>"presentations"}
PUT ... {:action=>"update", :controller=>"presentations"}
DELETE ... {:action=>"destroy", :controller=>"presentations"}
为什么没有 name_helper 用于显示操作?
我可以通过执行以下操作来覆盖问题:
resources :suppliers do
resource :presentation, :except => :show do
get "" => "presentations#show", as: "presentation"
end
end
给出路线:
presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}
但现在我们都知道这不是处理它的正确方法..
有什么建议吗?
-
(已编辑)
supplier_presentation_path(@supplier)
确实有效,但是为什么?...在我的 shell 上执行 rake paths
时它不会出现...
I have a has_one relation:
# supplier.rb
has_one :presentation
...
# presentation.rb
belongs_to :supplier
...
and the folowing nested routes for them:
# routes.rb
resources :suppliers do
resource :presentation
end
running rake routes
gives:
supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
GET ... {:action=>"show", :controller=>"presentations"}
PUT ... {:action=>"update", :controller=>"presentations"}
DELETE ... {:action=>"destroy", :controller=>"presentations"}
Why no name_helper for the show action?
I can override the problem doing something like:
resources :suppliers do
resource :presentation, :except => :show do
get "" => "presentations#show", as: "presentation"
end
end
giving the route:
presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}
but we all now that's not the right way to deal with it..
ANY SUGGESTIONS?
--
(edited)
supplier_presentation_path(@supplier)
does work, but why?... It doesn't appear when rake routes
is executed on my shell...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的不知道为什么当您执行
rake paths
时它不显示,但是您是否尝试在代码中执行supplier_presentation_path(@supplier)
?它应该根据您的路线工作。I dont really know why it's not displayed when you do
rake routes
but did you try in your code to dosupplier_presentation_path(@supplier)
? It should work based on your routes.尽管如此,它应该对你有用。试试这个:
或者
Never the less it should work for you. Try this:
or