Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Active Admin form inputs for has_and_belongs_to_many relationship
    text
    copied!<p>I have a very simple model</p> <pre><code>class Lifestyle &lt; ActiveRecord::Base attr_accessible :name has_and_belongs_to_many :profiles end </code></pre> <p>that has a <code>has_and_belongs_to_many</code> relationship with <code>Profile</code></p> <pre><code>class Profile &lt; ActiveRecord::Base attr_accessible ... belongs_to :occupation has_and_belongs_to_many :lifestyles accepts_nested_attributes_for :lifestyles end </code></pre> <p>I want to use ActiveAdmin to edit the Profile object, but also assign Lifestyles to a profile. It should be similar to dealing with <code>belongs_to :occupation</code>, as this is sorted out automatically by ActiveAdmin to a dropbox with the options pre-filled with available occupations. </p> <p>I've tried to use the <code>has_many</code> form builder method, but that only got me to show a form to type in the name of the Lifestyle and on submission, it returned an error.</p> <pre><code> f.object.lifestyles.build f.has_many :lifestyles do |l| l.input :name end </code></pre> <p>Error I get:</p> <pre><code>Can't mass-assign protected attributes: lifestyles_attributes </code></pre> <p><strong>The perfect way for me would be to build several checkboxes</strong>, one for each Lifestyle in the DB. Selected means that the lifestyle is connected to the profile, and unselected means to delete the relation. </p> <p>I'm having great doubts that this is possible using ActiveAdmin and without having to create very complex logic to deal with this. <strong>I would really appreciate it if you'd give your opinion and advise me if I should go this way or approach it differently.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload