2016-02-18 59 views
0

我有兩個型號,has_and_belongs_to_many關係Rails的主動管理複選框

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

根據軌道命名約定, 'has_and_belongs_to_many' 應具有複數assosciations。

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

This link may be helpful to you.

+0

嗨Sravan,感謝您的答覆。我已將其更改爲複數形式,但它仍未顯示選定的選項。還有什麼明顯的我錯過了嗎? – Sandeep

+0

我更新了'ActiveAdmin.register Article'中的代碼,請試試。 – Sravan

+0

你好,你試過了嗎? – Sravan