SectionsController 中的 NoMethodError#index 试图嵌套
NoMethodError in SectionsController#index trying to nest
我试图在我的训练代码中将 Sections
嵌套到 Page
中,但出现此错误:
undefined method `sections' for nil:NilClass
在此之前,我成功地将 Page
嵌套在 Subjects
中。
查看下图中试图将部分嵌套到页面中的错误。
这是我所做的:
sections_controller.rb:
class SectionsController < ApplicationController
layout 'admin'
before_action :confirm_logged_in
before_action :find_page
def index
@sections = @page.sections.sorted
end
private
def find_page
if params[:page_id]
@page = Page.find(params[:page_id])
end
end
end
我的看法:
index.html.erb:
<% @page_title = 'Sections' %>
<%= link_to('<< Back to Pages', {:controller => 'pages', :action => 'index', :subject_id => @page.subject_id}, :class => 'back-link') %>
<div class="sections index">
<h2>Sections</h2>
<%= link_to('Add New Section', {:action => 'new', :page_id => @page.id, :page_id => @page.id}, :class => 'action new') %>
<table class="listing" summary="Section list">
<tr class="header">
<th> </th>
<th>Page</th>
<th>Section</th>
<th>Content Type</th>
<th>Visible</th>
<th>Actions</th>
</tr>
<% @sections.each do |section| %>
<tr>
<td><%= section.position %></td>
<td><%= section.page.name if section.page %></td>
<td><%= section.name %></td>
<td><%= section.content_type %></td>
<td class="center"><%= status_tag(section.visible) %></td>
<td class="actions">
<%= link_to('Show', {:action => 'show', :id => section.id, :page_id => @page.id}, :class => 'action show') %>
<%= link_to('Edit', {:action => 'edit', :id => section.id, :page_id => @page.id}, :class => 'action edit') %>
<%= link_to('Delete', {:action => 'delete', :id => section.id, :page_id => @page.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
好了,一步步来吧;首先导航到 Sections index 路由,这是 index 操作的代码:
def index
@sections = @page.sections.sorted
end
您还有一个 before_action :find_page
应该设置 @page
:
def find_page
if params[:page_id]
@page = Page.find(params[:page_id])
end
end
但这永远不会发生,因为 params[:page_id]
是 nil 或者没有具有您传递的 ID 的页面。因此,@page 为 nil,您调用 nil.sections 并触发您发布的异常。
如何将 page_id
作为 url 参数传递?
有没有可能你真的打算在路线中做这样的事情:
resources :pages do
resources :sections
end
如果是这样,正确的路段索引路线将如下所示:
page_sections GET /pages/:page_id sections(.:format) sections#index
我试图在我的训练代码中将 Sections
嵌套到 Page
中,但出现此错误:
undefined method `sections' for nil:NilClass
在此之前,我成功地将 Page
嵌套在 Subjects
中。
查看下图中试图将部分嵌套到页面中的错误。
这是我所做的:
sections_controller.rb:
class SectionsController < ApplicationController
layout 'admin'
before_action :confirm_logged_in
before_action :find_page
def index
@sections = @page.sections.sorted
end
private
def find_page
if params[:page_id]
@page = Page.find(params[:page_id])
end
end
end
我的看法:
index.html.erb:
<% @page_title = 'Sections' %>
<%= link_to('<< Back to Pages', {:controller => 'pages', :action => 'index', :subject_id => @page.subject_id}, :class => 'back-link') %>
<div class="sections index">
<h2>Sections</h2>
<%= link_to('Add New Section', {:action => 'new', :page_id => @page.id, :page_id => @page.id}, :class => 'action new') %>
<table class="listing" summary="Section list">
<tr class="header">
<th> </th>
<th>Page</th>
<th>Section</th>
<th>Content Type</th>
<th>Visible</th>
<th>Actions</th>
</tr>
<% @sections.each do |section| %>
<tr>
<td><%= section.position %></td>
<td><%= section.page.name if section.page %></td>
<td><%= section.name %></td>
<td><%= section.content_type %></td>
<td class="center"><%= status_tag(section.visible) %></td>
<td class="actions">
<%= link_to('Show', {:action => 'show', :id => section.id, :page_id => @page.id}, :class => 'action show') %>
<%= link_to('Edit', {:action => 'edit', :id => section.id, :page_id => @page.id}, :class => 'action edit') %>
<%= link_to('Delete', {:action => 'delete', :id => section.id, :page_id => @page.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
好了,一步步来吧;首先导航到 Sections index 路由,这是 index 操作的代码:
def index
@sections = @page.sections.sorted
end
您还有一个 before_action :find_page
应该设置 @page
:
def find_page
if params[:page_id]
@page = Page.find(params[:page_id])
end
end
但这永远不会发生,因为 params[:page_id]
是 nil 或者没有具有您传递的 ID 的页面。因此,@page 为 nil,您调用 nil.sections 并触发您发布的异常。
如何将 page_id
作为 url 参数传递?
有没有可能你真的打算在路线中做这样的事情:
resources :pages do
resources :sections
end
如果是这样,正确的路段索引路线将如下所示:
page_sections GET /pages/:page_id sections(.:format) sections#index