【发布时间】:2016-05-01 17:31:13
【问题描述】:
问候我的程序员伙伴。我有 2 个问题。 1当然是我该如何解决这个问题。其次,这个错误是什么意思,所以对于我的下一次遭遇,我知道如何处理它。
我的 _form.html.erb
<%= error_messages_for(@section) %>
<table summary="Section form fields">
<tr>
<th><%= f.label(:page_id, "Page ID") %></th>
<td><%= f.select(:page_id, @pages.collect {|p| [p.name, p.id]}) %>
</tr>
<tr>
<th><%= f.label(:name) %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:position) %></th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th><%= f.label(:visible) %></th>
<td><%= f.select(:visible, 1..@section_count) %></td>
</tr>
<tr>
<th><%= f.label(:content_type) %></th>
<td>
<%= f.radio_button(:content_type, 'text') %> Text
<%= f.radio_button(:content_type, 'HTML') %> HTML
</td>
</tr>
<tr>
<th><%= f.label(:content) %></th>
<td><%= f.text_area(:content, :size => '40x10') %></td>
</tr>
</table>
我的部分控制器
class SectionsController < ApplicationController
layout "admin"
before_action :confirm_logged_in
before_action :find_page
def index
@sections = @page.sections.sorted
end
def show
@section = Section.find(params[:id])
end
def new
@section = Section.new({:name => "Default", :page_id => @page.id})
@section_count = Section.count +1
end
def create
@section = Section.new(section_params)
if @section.save
flash[:notice] = "Section created successfully."
redirect_to(:action => 'index', :page_id => @page.id)
else
@pages = @page.subject.pages.sorted
@section_count = Section.count +1
render('new')
end
end
def edit
@section = Section.find(params[:id])
end
def update
@section = Section.find(params[:id])
if @section.update_attributes(section_params)
flash[:notice] = "Section updated successfully."
redirect_to(:action => 'show', :id => @section.id, :page_id => @page.id)
else
@section_count = Section.count
render('edit')
end
end
def delete
@section = Section.find(params[:id])
end
def destroy
@section = Section.find(params[:id]).destroy
flash[:notice] = "Section destroyed successfully"
redirect_to(:action => 'index' , :page_id => @page.id)
end
private
def section_params
params.require(:section).permit(:page_id, :name, :position, :visible, :content_type, :content)
end
def find_page
if params[:page_id]
@page = Page.find(params[:page_id])
end
end
end
【问题讨论】:
-
嘿,将
@pages = @page.subject.pages.sorted添加到您的new操作中,因为@pages 在new中为零,所以它会给您这个错误 -
@VishalJAIN 我试图让您的评论成为主要答案。你的声望低吗?
-
@VishalJAIN 必须写下答案
标签: ruby-on-rails class methods null