Rails 3.1 has_one nested resource: routing not generating “all” paths

拥有回忆 提交于 2020-01-31 19:33:05

问题


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 routesgives:

    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...


回答1:


I dont really know why it's not displayed when you do rake routes but did you try in your code to do supplier_presentation_path(@supplier)? It should work based on your routes.




回答2:


Never the less it should work for you. Try this:

link_to "Presentation", [@suplier, @presentation]

or

link_to "Presentation", suplier_presentation_path(@suplier, @presentation)


来源:https://stackoverflow.com/questions/7029664/rails-3-1-has-one-nested-resource-routing-not-generating-all-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!