Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you handle serialized edit fields in an Active Admin resource?
    primarykey
    data
    text
    <p>I have a model, <code>Domain</code>, which has a text field, <code>names</code>. </p> <pre><code>&gt; rails g model Domain names:text invoke active_record create db/migrate/20111117233221_create_domains.rb create app/models/domain.rb &gt; rake db:migrate == CreateDomains: migrating ================================================== -- create_table(:domains) -&gt; 0.0015s == CreateDomains: migrated (0.0066s) ========================================= </code></pre> <p>I set this field as <strong>serialized</strong> into an array in the model. </p> <pre><code># app/models/domain.rb class Domain &lt; ActiveRecord::Base serialize :names, Array end </code></pre> <p>Create the ActiveAdmin resource for this model</p> <pre><code>&gt; rails g active_admin:resource Domain create app/admin/domains.rb </code></pre> <p>then, in the app/admin/domains.rb, I setup the various blocks to handle the serialized field as such</p> <pre><code># app/admin/domains.rb ActiveAdmin.register Domain do index do id_column column :names do |domain| "#{domain.names.join( ", " ) unless domain.names.nil?}" end default_actions end show do |domain| attributes_table do row :names do "#{domain.names.join( ", " ) unless domain.names.nil?}" end end end form do |f| f.inputs "Domain" do f.input :names end f.buttons end # before we save, take the param[:domain][:name] parameter, # split and save it to our array before_save do |domain| domain.names = params[:domain][:names].split(",") unless params[:domain].nil? or params[:domain][:names].nil? end end </code></pre> <p>Nearly everything works great -- my names are displayed as comma separated in the index and show views. When I update a record with my names field set to "a,b,c", the before_save works to turn that into an array that is then saved via the ActiveRecord serialize.</p> <p>What I can not solve is how to make the edit form put in a comma-separated list into the text field. I tried using a partial and using formtastic syntax directly as well as trying to make it work via the active_admin DLS syntax. Does anyone know how to make this work?</p> <p><strong>Specifically, if I have the following array saved in my domain.names field:</strong></p> <pre><code># array of names saved in the domain active_record domain.names = ["a", "b", "c"] </code></pre> <p><strong>how to change:</strong></p> <pre><code> form do |f| f.inputs "Domain" do f.input :names end f.buttons end </code></pre> <p><strong>so that when the <code>edit</code> form is loaded, in the text field instead of seeing <code>abc</code>, you see <code>a,b,c</code>.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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