Rails Activeadmin has many relation items

Rails activeadmin gem very solid and it just works once it is installed. But it takes a bit effort if we try to customize it. The customization work can tiresome if it is not done right. In this post we want to show how we can display has many relational items in an activeadmin form. Lets consider we have a gallery and each gallery can have multiple images. The gallery model has a has_many relation with gallery_images. Now if we want to add or edit using activeadmin we will see the following code:
 
  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "Gallery Name" do
      f.input :name
    end
    f.actions
  end
Now if we need to pull all the has many relational items then we can easily do it like below:
 
  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "Gallery Name" do
      f.input :name
    end
    f.has_many :gallery_images, new_record: false do |app_f|
      app_f.input :filename, :hint => image_tag(app_f.object.filename.url,
 width: '200', item_id: app_f.object.id)
    end
    f.actions
  end

Comments