Search code examples
ruby-on-railsrubyselectsimple-form-for

Simple form for custom selected


I have 2 models Website and Ad_tag

website has_many ad_tags

ad_tag belongs_to website

in my ad_tag I have bool field called 'attached'

I need to create simple_form_for multiple select where if attached is true - line will be select, but I can't. help me, please

my tries:

= simple_form_for @website, method: :put do |f|
  = f.input :ad_tag_ids, collection: @website.ad_tags.google.map { |a| [a.name, a.attached ? a.id : nil] }, as: :select, input_html: { multiple: true, size: 30 }
  = f.button :submit, class: 'btn-primary' 

But this method can't set ids for unattached records.


Solution

  • I did it with rails form helpers. (Also I needed to group them into categories.

      = f.select :ad_tag_ids,  grouped_options_for_select(@website.ad_tags.google.map { |a| [a.name, a.google_meta['status'], a.id, { selected: a.attached }]}.group_by(&:second).sort), { prompt: 'Please select'}, { multiple: true, size: 30, class: 'form-control select optional' }