Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails routing show action nested resources

Yeah the problem is, that I created a nested resource like this:

resources :albums do 
  resources :elements
end

and the rake routes command displays:

album_element GET /albums/:album_id/elements/:id(.:format) elements#show

So when I am at .../albums/1 I can head for .../albums/1/elements

This starts the index action of the elements controller, just fine. But if I edit the index.html.erb to

<%= link_to 'Show', album_element_path %>

I got an error like this:

Started GET "/albums/1/elements" for 176.221.47.67 at Tue Oct 09 14:25:39 +0200 2012
Processing by ElementsController#index as HTML
Parameters: {"album_id"=>"1"}
Rendered elements/index.html.erb within layouts/application (9.2ms)
Completed 500 Internal Server Error in 123ms

ActionController::RoutingError (No route matches {:controller=>"elements", :action=>"show"}):
app/views/elements/index.html.erb:29:in `_app_views_elements_index_html_erb___13604879__168097178'
app/views/elements/index.html.erb:18:in `each'
app/views/elements/index.html.erb:18:in `_app_views_elements_index_html_erb___13604879__168097178'
app/controllers/elements_controller.rb:7:in `index'

So it says that no route matches ... but I actually have that in my rake routes displayed ? What am I doing wrong ?

like image 502
user1697061 Avatar asked Oct 09 '12 13:10

user1697061


1 Answers

You need to provide the two neccesary arguments for album_element_path:

<%= link_to 'Show', album_element_path(@album, @element) %>
like image 75
Hugo Logmans Avatar answered Oct 05 '22 11:10

Hugo Logmans