1

我在 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)
4

1 回答 1

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
于 2013-10-09T15:50:07.053 回答