我在Active Admin中创建了一个Polymorphic Association,用于创建与Industry关联的Keyword。我可以在Active Admin中显示关联(关联是在控制台中创建的),但是当我更新并创建New时,只传递关键字参数。如果我返回控制台并进行更新,则更新后的数据会在Active Admin中正确显示。
我从未收到错误。
我有:
class Industry < ActiveRecord::Base
attr_accessible :name, :keywords, :keywords_attributes
# Associations
has_many :profiles
has_many :companies
has_many :users
has_many :keywords, as: :keyable
accepts_nested_attributes_for :keywords
end
class Keyword < ActiveRecord::Base
attr_accessible :name, :profile_id, :active,
:keyable_attributes, :rating,
:keyable_id, :keyable_industry
# Associations
belongs_to :profile # FK
belongs_to :keyable, polymorphic: true
accepts_nested_attributes_for :keyable
attr_accessor :keyable_industry
def keyable_industry
self.keyable.id if self.keyable.is_a? Industry
end
end
ActiveAdmin.register Keyword do
index do
column :name
column :active
column :rating
column "Keyword Group", :keyable
column :keyable_type
default_actions
end
form do |f|
f.inputs "Conference Detail" do
f.input :name
f.input :active
f.input :rating
f.input :keyable_industry
end
f.inputs "Industry" do
f.input :keyable_industry, label: "Industry",
:as => :select,
:collection => Industry.all.map {|i| [i.name]},
:include_blank => false
end
f.actions
end
end
Development Log Output
Started PUT "/admin/keywords/2" for 127.0.0.1 at 2013-09-13 11:41:32 -0400
Processing by Admin::KeywordsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"nnbMIHy1YndWNOKyF+LSABYyeQMpKQAiTyqGbL2sq3g=", "keyword"=> {"name"=>"test", "active"=>"1", "rating"=>"1", "keyable_industry"=>"Agriculture & Forestry"}, "commit"=>"Update Keyword", "id"=>"2"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Keyword Load (0.7ms) SELECT "keywords".* FROM "keywords" WHERE "keywords"."id" = $1 LIMIT 1 [["id", "2"]]
(0.7ms) BEGIN
(1.1ms) UPDATE "keywords" SET "name" = 'test', "updated_at" = '2013-09-13 15:41:32.287147' WHERE "keywords"."id" = 2
(2.9ms) COMMIT
Redirected to http://blog.dev/admin/keywords/2
Completed 302 Found in 14ms (ActiveRecord: 0.0ms)
答案 0 :(得分:1)
好的,我得到了这个工作并不确定它是否是最好的解决方案。意见欢迎。
要查看,我的模型有以下内容:
class Industry < ActiveRecord::Base
attr_accessible :name, :keywords, :keywords_attributes
# Associations
has_many :profiles
has_many :companies
has_many :users
has_many :keywords, as: :keyable
accepts_nested_attributes_for :keywords
end
class Keyword < ActiveRecord::Base
attr_accessible :name, :profile_id, :active,
:keyable_attributes, :rating,
:keyable_id, :keyable_industry, :keyable_type
# Associations
belongs_to :profile # FK
belongs_to :keyable, polymorphic: true
accepts_nested_attributes_for :keyable
has_many :conference_keywordeds
has_many :conferences, through: :conference_keywordeds
end
因此,对于我的ActiveAdmin关键字控制器,我为Industry创建了一个隐藏值来传递该值。现在我现在有了:
ActiveAdmin.register Keyword do
index do
column :name
column :active
column "Keyword Type", :keyable_type
column "Keyword Group", :keyable
default_actions
end
show do |keyword|
attributes_table do
row :id
row :name
row :created_at
row :updated_at
row :active
row :keyable_id
row :keyable_type
end
active_admin_comments
end
form do |f|
f.inputs "Conference Detail" do
f.input :name
f.input :active
f.input :keyable,
:label => "Keyword Group", as: :select,
:collection => Industry.all,
:include_blank => false
f.input :keyable_type, as: :hidden,
:input_html => { :value => "Industry"}
end
f.actions
end
end