5

我是一个 Rails 菜鸟,所以下面可能是由于缺乏理解,但是我整天都在看/阅读,似乎找不到解决方案。

我有两个模型 - 项目和技术:

项目 :

class Project < ActiveRecord::Base

  attr_accessible description, :name

  has_and_belongs_to_many :technologies, :join_table => :projects_technologies

end

技术 :

class Technology < ActiveRecord::Base

  attr_accessible :abbr, :description, :name

  has_and_belongs_to_many :projects, :join_table => :projects_technologies

end

我的 Create_Projects_Technologies 迁移如下:

class CreateProjectsTechnologies < ActiveRecord::Migration
  def self.up

    create_table :projects_technologies, :id => false do |t|
        t.references :project
        t.references :technology
    end
    add_index :projects_technologies, [:project_id, :technology_id]
    add_index :projects_technologies, [:technology_id, :project_id]
  end

  def self.down
    drop_table :projects_technologies
  end
end

然后,我使用 Active Admin 使用以下表单创建和编辑项目模型:

ActiveAdmin.register Project do

  form do |f|
    f.inputs "Project attributes" do
      f.input :name
      f.input :description
      f.input :technologies, as: :check_boxes
    end
    f.buttons
  end

end

这正确地将我的所有技术显示为复选框,但是一旦我提交表单,我就会遇到以下我无法克服的错误:

ActiveModel::MassAssignmentSecurity::Admin::ProjectsController#update 中的错误

Can't mass-assign protected attributes: technology_ids

非常感谢所有帮助:D

4

1 回答 1

7

简单地将 technology_ids 添加到项目 attr_accessible

attr_accessible :client, :description, :name, :technology_ids
于 2012-08-12T19:37:12.737 回答