6

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 ?

1 Answer 1

7

You need to provide the two neccesary arguments for album_element_path:

<%= link_to 'Show', album_element_path(@album, @element) %>
4
  • Thanks for the quick answer but I get another error then. Probalby I need to set @albums ? but where ? I changed it to <%= link_to 'Show', album_element_path(@album, @element) %> And added in the show method of the element controller: @album= Album.find(params[:album_id]) Oct 9, 2012 at 13:32
  • 1
    If anyone is interested in the solution, it is: <%= link_to 'Show', album_element_path(@album, element) %> Because I was in a for each - and had to use element instead of @element. Oct 9, 2012 at 14:24
  • @element probably needs to be replaced by the iterator you use in index.html.erb Oct 9, 2012 at 14:25
  • Hm now I added <%= link_to 'Edit', edit_album_element_path(@album, element) %> But when I click on it, I get: ActionView::Template::Error (undefined method `element_path' for #<#<Class:0xebde8fa0>:0xebde59f4>): 1: <%= form_for(@element) do |f| %> Do I have to fix this @element somehow ? Oct 9, 2012 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.