Formtastic Datalist Activeadmin not saving

505 views Asked by At

I am using the activeadmin gem with formtastic I want to add a datalist to a particular text field.

#FILE app/admin/model.rb
ActiveAdmin.register Model do
  permit_params :title, :article_headline_a_id, :article_headline_b_id

  form(html: { multipart: true }) do |f|
    f.input :article_headline_a_id, :as => :datalist, :collection => Article.pluck(:title)
    f.input :article_headline_b_id, :as => :datalist, :collection => Article.pluck(:title)
    f.actions
  end 
end

Everything seems to work fine until I save and check the console I get article_a_id = 0 instead of the expected id number.

#model.rb
  belongs_to :article1, :foreign_key => 'article_headline_a_id', :class_name => "Article"
  belongs_to :article2, :foreign_key => 'article_headline_b_id', :class_name => "Article"
1

There are 1 answers

3
MGrantley On

Sounds like you're not mapping the ID to the title try:

    f.input :article_headline_a_id, :as => :datalist, :collection => Article.pluck(:title, :id)