Ruby 在 Rails 上 4.2.6 未定义的方法 page_id
Ruby on Rails 4.2.6 Undefined Method page_id
这是我收到的错误:#Page:0x0000000b2c1e28.
的未定义方法“page_id”
显示 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
我正尝试在我的内容管理系统的页面中嵌套部分。
这是我的部门表格:
<%= 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>
这是我的版块控制器:
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:这是我在Whosebug上的第一个问题,所以如果有明显的错误请原谅我。这个问题几乎与 this 完全相同,但我的问题是创建一个新部分而不是它。
当您看到类似 undefined method 'page_id'
的内容时,其中方法是数据库列名,通常意味着数据库中缺少该列 table--- 通常是因为您没有然而 运行 适当的迁移。
在你的情况下,这不是因为你没有 运行 迁移,而是因为 Rails 查找错误 table!请注意,您在 Page
上调用 page_id
,但它应该是 Section
:
undefined method `page_id' for #Page:0x0000000b2c1e28.
您没有在视图中显示您的 form_for
,但您应该确保它是 form_for @section
而不是 form_for @page
。
此外,它超过第 5 行的原因是因为 f.label
不需要调用 page_id
方法,但 f.select
需要调用(以获取当前值)。
这是我收到的错误:#Page:0x0000000b2c1e28.
的未定义方法“page_id”显示 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
我正尝试在我的内容管理系统的页面中嵌套部分。
这是我的部门表格:
<%= 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>
这是我的版块控制器:
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:这是我在Whosebug上的第一个问题,所以如果有明显的错误请原谅我。这个问题几乎与 this 完全相同,但我的问题是创建一个新部分而不是它。
当您看到类似 undefined method 'page_id'
的内容时,其中方法是数据库列名,通常意味着数据库中缺少该列 table--- 通常是因为您没有然而 运行 适当的迁移。
在你的情况下,这不是因为你没有 运行 迁移,而是因为 Rails 查找错误 table!请注意,您在 Page
上调用 page_id
,但它应该是 Section
:
undefined method `page_id' for #Page:0x0000000b2c1e28.
您没有在视图中显示您的 form_for
,但您应该确保它是 form_for @section
而不是 form_for @page
。
此外,它超过第 5 行的原因是因为 f.label
不需要调用 page_id
方法,但 f.select
需要调用(以获取当前值)。