Rails Active Admin复选框不显示所选选项

时间:2016-02-18 12:46:08

标签: ruby-on-rails activeadmin

我有两个与has_and_belongs_to_many关系的模型

class ArticleRejectionReason < ActiveRecord::Base

attr_accessible :reason 
has_and_belongs_to_many :articles
end

class Article < ActiveRecord::Base
has_and_belongs_to_many :article_rejection_reasons
end


ActiveAdmin.register Article do
  permit_params article_rejection_reason_ids: []

  f.inputs "Article Details" do
   f.input :article_rejection_reasons, as: :check_boxes, collection: ArticleRejectionReason.all.collect { |r| [r.reason, r.id] }
 end
end

按预期生成复选框(选项)。我可以将数据保存到数据库中。

然而,当我点击“编辑”按钮时,我看不到显示的选定选项。

我知道如何在jQuery中执行此操作。如何在活动管理员中显示所选选项?

更新1:

mysql> select * from article_rejection_reasons_articles;
+------------+-----------------------------+
| article_id | article_rejection_reason_id |
+------------+-----------------------------+
|        386 |                           2 |
|        386 |                           4 |
|        386 |                           6 |
+------------+-----------------------------+

更新2: Result

1 个答案:

答案 0 :(得分:1)

根据rails命名惯例,&#39; has_and_belongs_to_many&#39;应该有多个assosciations。

&#13;
&#13;
class ArticleRejectionReason < ActiveRecord::Base

attr_accessible :reason 
has_and_belongs_to_many :articles /* plural */
end

class Article < ActiveRecord::Base
has_and_belongs_to_many :article_rejection_reasons /* plural */
end


ActiveAdmin.register Article do
  permit_params article_rejection_reason_ids: []
  form do |f|
  f.inputs "Article Details" do
   f.input :article_rejection_reasons, as: :check_boxes, collection: ArticleRejectionReason.all.collect { |r| [r.reason, r.id] }
  end
 end
end
&#13;
&#13;
&#13;

This link may be helpful to you.