Reputation: 2085
When I try to go to this url: http://localhost:3000/lessons/18/pages
, I get this error:
No route matches {:action=>"show", :controller=>"pages", :lesson_id=>#<Page id: 5,
title: "Page One", page_num: 1, lesson_id: 18, stats_id: nil, sources_id: nil,
created_at: "2012-04-06 04:55:39", updated_at: "2012-04-06 04:55:39">}
I have two tables: lessons, pages
page.rb
belongs_to :lessons
lessons.rb
has_many :pages
routes.rb
resources :lessons do
resources :pages
end
The pages index looks like this:
<% @lesson.pages.each do |page| %>
<tr>
<td><%= page.title %></td>
<td><%= page.page_num %></td>
<td><%= page.lesson_id %></td>
<td><%= page.stats_id %></td>
<td><%= page.sources_id %></td>
<td><%= link_to 'Show', lesson_page_path(page) %></td>
<td><%= link_to 'Edit', edit_page_path(page) %></td>
<td><%= link_to 'Destroy', lesson_page, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
And here is the pages controller:
def index
@lesson = Lesson.find(params[:lesson_id])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @pages }
end
end
Why is the show
route not working?
Upvotes: 0
Views: 1537
Reputation: 97004
With nested resource routes you need to pass all the objects in the route:
lesson_page_path(@lesson, page)
The error is somewhat revealing of this in that it shows that it's trying to get the :lesson_id
from a Page
instance.
Upvotes: 3