没有路线匹配{:action =>“show”,:controller =>“pages”}

时间:2012-04-07 23:50:05

标签: ruby-on-rails routes

当我尝试访问此网址时:http://localhost:3000/lessons/18/pages,我收到此错误:

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">}

我有两张桌子:课程,页面

page.rb

belongs_to :lessons

lessons.rb

has_many :pages

的routes.rb

resources :lessons do
   resources :pages
end

页面索引如下所示:

<% @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 %>

这是页面控制器:

def index
 @lesson = Lesson.find(params[:lesson_id])
    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @pages }
  end
end

为什么show路线不起作用?

1 个答案:

答案 0 :(得分:3)

使用嵌套资源路由,您需要传递路径中的所有对象:

lesson_page_path(@lesson, page)

错误在某种程度上揭示了这一点,因为它表明它正试图从:lesson_id实例中获取Page