无法获取视图以保存关联数据

时间:2013-09-17 19:09:18

标签: ruby-on-rails has-and-belongs-to-many

我需要做些什么来使创建动作有效?

我有一个带有has_and_belongs_to_many关联的表。 “新”页面工作正常,但当我选择它并尝试保存它会引发错误:

无法批量分配受保护的属性:book_id

我试着设置:

config.active_record.whitelist_attributes = false

但它没有改变任何东西

我的模特:

class Book < ActiveRecord::Base
  has_and_belongs_to_many :orbs

  attr_accessible :dataf, :datai, :descr, :nome

  validates :nome, uniqueness: true, presence: true
end

class Orb < ActiveRecord::Base
  belongs_to :orb_type
  has_and_belongs_to_many :books

  attr_accessible :descr, :nome, :orb_type_id

  validates :nome, uniqueness: true, presence: true
end

我的控制器:

  def create
    @orb = Orb.new(params[:orb])

    respond_to do |format|
      if @orb.save
        format.html { redirect_to @orb, notice: 'Orb was successfully created.' }
        format.json { render json: @orb, status: :created, location: @orb }
      else
        format.html { render action: "new" }
        format.json { render json: @orb.errors, status: :unprocessable_entity }
      end
    end
  end

此外,任何人都可以告诉我,当按下“编辑”时,我需要做些什么才能检查自动装箱 我是铁杆新手。 THX!

编辑: 将attr_accessible:book_id添加到我的orb模型会引发错误:

unknown attribute: book_id

它在控制台上使用&lt;&lt;操作

1 个答案:

答案 0 :(得分:0)

您需要在您的模型Orb上声明book_id可访问

attr_accessible :descr, :nome, :orb_type_id, :book_id

编辑对象时,如果布尔值的复选框名称正确(与db字段相同),则应根据其值在视图上显示为已选中或未选中。

相关问题