我試圖巢Sections
到Page
在我的訓練代碼,我得到這個錯誤:NoMethodError在SectionsController#指數試圖巢
undefined method `sections' for nil:NilClass
在此之前,我嵌套我在Subjects
Page
成功。
看到下面的圖片中的錯誤試圖嵌套到頁面的章節。
這裏是我做了什麼:
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>
從哪個頁面,你想調用你的分區控制器的'index'動作?即您在發生錯誤之前點擊的鏈接。你能告訴我們你的鏈接代碼嗎? –
@SunnyK,我用這個URL直接調用它。 http://127.0.0.1:3000/sections –
如果我刪除所有嵌套,它的作品。作爲CMS,我將部分嵌套到Page中,以便在CMS中只有相關記錄。讓我粘貼沒有嵌套的代碼,其中一旦我http://127.0.0.1:3000/sections –