Ruby on rails 节控制器#索引中的NoMethodError尝试嵌套
我试图将Ruby on rails 节控制器#索引中的NoMethodError尝试嵌套,ruby-on-rails,content-management-system,Ruby On Rails,Content Management System,我试图将部分嵌套到我的培训代码中的页面,出现以下错误: undefined method `sections' for nil:NilClass 在此之前,我成功地将我的页面嵌套在主题中 请参阅下面图片中的错误,尝试将节嵌套到页面中 以下是我所做的: 第_controller.rb节: class SectionsController < ApplicationController layout 'admin' before_action :confirm_logged_i
部分
嵌套到我的培训代码中的页面
,出现以下错误:
undefined method `sections' for nil:NilClass
在此之前,我成功地将我的页面
嵌套在主题
中
请参阅下面图片中的错误,尝试将节嵌套到页面中
以下是我所做的:
第_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>
'页面',:action=>'索引',:subject\u id=>@page.subject\u id},:class=>'反向链接')%>
小节
'new',:page_id=>@page.id,:page_id=>@page.id},:class=>'action new')%>
页
部分
内容类型
看得见的
行动
'show',:id=>section.id,:page_id=>@page.id},:class=>'action show')%>
‘编辑’,:id=>section.id,:page_id=>@page.id},:class=>'action edit')%>
'delete',:id=>section.id,:page_id=>@page.id},:class=>'action delete')%>
好的,让我们一步一步走;首先,导航到节索引路径,这是索引操作的代码:
def index
@sections = @page.sections.sorted
end
您还有一个在\u操作之前:find \u 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,您发布的异常被触发
如何将页面id
作为url参数传递
你是否真的打算在路线上做这样的事情:
resources :pages do
resources :sections
end
如果是这样,正确的区段索引路线如下所示:
page_sections GET /pages/:page_id sections(.:format) sections#index
您试图从哪个页面调用分区控制器的
索引
操作?即,在出现错误之前单击的链接。你能给我们看一下你的链接代码吗?@SunnyK,我用这个URL从我的路线直接呼叫它。如果我移除所有嵌套,它就会工作。作为一个CMS,我将部分嵌套到页面中,以便在我的CMS中只有相关记录。让我粘贴代码而不嵌套,这在我没有在url中传递任何页面id时起作用,这就是@page为nil的原因。请看下面我的答案。您或者需要在页面中嵌套这些节,或者需要像这样调用url:@bosskovic可以正常工作,没有任何错误。但我仍然喜欢工作。你有什么建议吗?很好的洞察力。完成了工作并填充了我的分区页面。但我还是希望能发挥作用。这是我的全部路线。rb get'admin',:to=>'access#index'match':controller(/:action(/:id)),:via=>[:get,:post]您有一个解决方案,但我仍然无法在这里找到解决方案。请写下您尝试的内容和错误。你调整路线了吗?