Search code examples
ruby-on-railsrubyruby-on-rails-5rails-admin

Display multiple images in rails_admin


I am using rails_admin with Rails 5. I have a model Hotel with an association images (has_many). Currently I use the default config of rails_admin, and under Hotel show page, the images of a hotel are displayed in this format:

Image #5381, Image #5382, Image #5383, Image #5384, Image #5385,...

How can I display these images as a gallery given that each image has a thumbnail_url attribute? I means which config I could put in this block to change the display:

show do
  field :images do
    # Display as a gallery
  end
end

Thanks for your time!


Solution

  • Rendering a custom partial is probably the best approach here. Something like:

    field :images do
      render do
        bindings[:view].render partial: 'image_preview', locals: {object: self}
      end
    end
    

    Then create the partial in app/views/rails_admin/main/_image_preview.html.*, and you're free to control what appears.