我在 Active Admin 中创建了一个多态关联,用于创建与行业相关的关键字。我可以在 Active Admin 中很好地显示关联(关联是在控制台中创建的)但是当我更新并创建新时,只有关键字参数被传递。如果我返回控制台并更新,更新的数据会正确显示在 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)