首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 4.2.6未定义方法page_id

Rails 4.2.6未定义方法page_id
EN

Stack Overflow用户
提问于 2016-06-15 13:39:05
回答 1查看 392关注 0票数 0

这是我得到的错误:#Page:0x0000000b2c1e28的未定义方法`page_id‘。

显示第6行所显示的C:/Users/eiria/Sites/simple_cms/app/views/sections/_form.html.erb:

用于#跟踪模板包含的未定义方法`page_id‘:app/视图/节/new.html.erb

Rails.root: C:/User/eiria/Site/simple_cms

我正在尝试为我的内容管理系统在页面中嵌套部分。

这是我的章节表格:

代码语言:javascript
复制
<%= 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>

这位是我的部门控制器:

代码语言:javascript
复制
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

分段模式:

代码语言:javascript
复制
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

页面模型:

代码语言:javascript
复制
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行使用相同的方法,但显然没有什么问题.

任何帮助都将不胜感激。

这是我关于堆叠溢出的第一个问题,所以请原谅我任何明显的错误。这个问题与this几乎完全相同,但我的问题是创建一个新的部分,而不是它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-15 16:11:48

当您看到类似undefined method 'page_id'的东西,其中的方法是数据库列名时,它通常意味着该列从数据库表中丢失--最常见的原因是您尚未运行适当的迁移。

在您的例子中,这并不是因为您没有运行迁移,而是因为Rails找错了表!请注意,您是在一个page_id上调用Page,但它应该是一个Section

代码语言:javascript
复制
undefined method `page_id' for #Page:0x0000000b2c1e28.

您没有在视图中显示您的form_for,但是您应该确保它是form_for @section而不是form_for @page

另外,它通过第5行的原因是,f.label不需要调用page_id方法,但是f.select需要调用(以获得当前值)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37837145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档