问题
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...
回答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