Ter*_*unk 5 ruby-on-rails has-and-belongs-to-many ruby-on-rails-3.1
我是一个铁杆菜鸟,所以下面可能是缺乏理解,但我一直在寻找/阅读整天,似乎无法找到解决方案.
我有两个模型 - 项目和技术:
项目:
class Project < ActiveRecord::Base
attr_accessible description, :name
has_and_belongs_to_many :technologies, :join_table => :projects_technologies
end
Run Code Online (Sandbox Code Playgroud)
技术:
class Technology < ActiveRecord::Base
attr_accessible :abbr, :description, :name
has_and_belongs_to_many :projects, :join_table => :projects_technologies
end
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
然后,我使用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
Run Code Online (Sandbox Code Playgroud)
这正确地将我的所有技术显示为复选框,但是一旦我提交表单,我就会遇到以下错误,我无法克服:
ActiveModel :: MassAssignmentSecurity :: Admin :: ProjectsController#update中的错误
Can't mass-assign protected attributes: technology_ids
Run Code Online (Sandbox Code Playgroud)
所有的帮助非常感谢:D
小智 7
简单地将technology_id添加到Project attr_accessible
attr_accessible :client, :description, :name, :technology_ids
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1599 次 |
| 最近记录: |