这是我得到的错误:未定义的方法`page_id'对于#Page:0x0000000b2c1e28。
显示C:/Users/eiria/Sites/simple_cms/app/views/sections/_form.html.erb第6行:
未定义的方法`page_id'为# 模板包含跟踪:app / views / sections / new.html.erb
Rails.root:C:/ Users / eiria / Sites / simple_cms
我试图在我的内容管理系统的页面中嵌套Sections。
这是我的章节表格:
<%= error_messages_for(@section) %>
<table summary="Section form fields">
<tr>
<th><%= f.label(:page_id, "Page") %></th>
<td><%= f.select(:page_id, @pages.map {|s| [s.name, s.id]}) %></td>
</tr>
<tr>
<th><%= f.label(:name) %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:position) %></th>
<td><%= f.select(:position, 1..@section_count) %></td>
</tr>
<tr>
<th><%= f.label(:visible) %></th>
<td><%= f.select(:visible, {"Visible" => 1, "Hidden" => 2}) %></td>
</tr>
<tr>
<th><%= f.label(:content_type) %></th>
<td><%= f.select(:content_type, ['text', 'HTML']) %></td>
</tr>
<tr>
<th><%= f.label(:content) %></th>
<td><%= f.text_area(:content, :size => '40x10') %></td>
</tr>
</table>
这是我的Sections控制器:
class SectionsController < ApplicationController
layout "admin"
before_action :confirm_logged_in
before_action :find_page
def index
@sections = @page.sections.sorted
@subject = @page.subject
end
def show
@section = Section.find(params[:id])
end
def new
@section = Section.new({:page_id => @page.id, :name => "Default"})
@pages = Page.order('position ASC')
@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.order('position ASC')
@section_count = Section.count + 1
render('new')
end
end
def
@section = Section.find(params[:id])
@pages = Page.order('position ASC')
@section_count = Section.count
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 = Section.find(params[:id])
@pages = Page.order('position ASC')
@section_count = Section.count
render('')
end
end
def delete
@section = Section.find(params[:id])
end
def destroy
section = Section.find(params[:id]).destroy
flash[:notice] = "Section '#{section.name}' destroyed succsessfully."
redirect_to(:action => 'index')
end
private
def section_params
params.require(:section).permit(:page_id, :name, :permalink, :position, :visible, :content_type, :content)
end
def find_page
if params[:page_id]
@page = Page.find(params[:page_id])
@subject = @page.subject
end
end
end
部分模型:
class Section < ActiveRecord::Base
belongs_to :page
has_many :section_edits
has_many :editors, :through => :section_edits, :class_name => "AdminUser"
CONTENT_TYPES = ['text', 'HTML']
validates_presence_of :name
validates_length_of :name, :maximum => 255
validates_inclusion_of :content_type, :in => CONTENT_TYPES,
:message => "must be one of: #{CONTENT_TYPES.join(', ')}"
validates_presence_of :content
scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("sections.position ASC") }
scope :newest_first, lambda { order("sections.created_at DESC") }
end
页面模型:
class Page < ActiveRecord::Base
belongs_to :subject
has_many :sections
has_and_belongs_to_many :editors, :class_name => "AdminUser"
validates_presence_of :name
validates_length_of :name, :maximum => 255
validates_presence_of :permalink
validates_length_of :permalink, :within => 3..255
validates_uniqueness_of :permalink
scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("pages.position ASC") }
scope :newest_first, lambda { order("pages.created_at DESC") }
end
我不明白为什么它会为第6行说明未定义的方法page_id,而第5行则使用相同的方法,但是那里显然没有错...
任何帮助都将不胜感激。
P.S:这是我关于stackoverflow的第一个问题,所以请原谅我任何明显的错误。这个问题与this几乎完全相同,但我的问题是创建一个新的部分,而不是它。
答案 0 :(得分:3)
当您看到类似undefined method 'page_id'
的内容时,该方法是数据库列名称,通常意味着数据库表中缺少该列 - 通常是因为您还没有运行相应的迁移。
在你的情况下,这不是因为你没有进行迁移,而是因为Rails正在寻找错误的表格!请注意,您在page_id
上呼叫Page
,但它应该是Section
:
undefined method `page_id' for #Page:0x0000000b2c1e28.
您未在视图中显示form_for
,但应确保其为form_for @section
而非form_for @page
。
此外,它越过第5行的原因是因为f.label
不需要调用page_id
方法,但是f.select
会执行(以获取当前值)。