Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created February 21, 2014 08:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevenyap/9130855 to your computer and use it in GitHub Desktop.
Save stevenyap/9130855 to your computer and use it in GitHub Desktop.
Rails Admin

Extracting model configuration

# app/models/booking.rb
class Booking
  include RailsAdmin::Booking
end

# app/models/concerns/rails_admin/booking.rb
module RailsAdmin::Booking
  extend ActiveSupport::Concern

  included do
    rails_admin do
      navigation_label 'Booking'
    end
  end
end

# config/initializers/rails_admin.rb
# You need to whitelist the model
config.included_models = ['Booking']

Add Multiple Photos Upload

  • For example, to add multiple images to a product
# Product model
has_many :images, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true,
                                reject_if: :all_blank
                                
# Image model
class Image < ActiveRecord::Base
  belongs_to :product, inverse_of: :images

  has_attached_file :photo,
                    whiny: false,
                    default_url: "/images/:style/missing.png",
                    default_style: :normal,
                    styles: { normal: '210x110>' }
end

Delete Paperclip Images

# In your model
# where you have has_attached_file :image

attr_accessor :delete_image
before_validation { self.image.clear if self.delete_image == '1' }

Fieldsets

module RailsAdmin::Ship
  extend ActiveSupport::Concern

  included do
    rails_admin do
      object_label_method :name
      navigation_label 'Ships'
      weight -100

      edit do
        include_all_fields
        exclude_fields :categories, :cabins

        group :deck do
          label "Deck Info"
          active false
          field :deck_image
        end

        group :dining do
          label "Dining Info"
          active false
          field :dining_info, :wysihtml5
        end

        group :images do
          label "Photo Gallery"
          active false
          field :images do
            active true
          end
        end
      end
    end
  end
end

Nested form ordering

  • You want to order your accepts_nested_attributes nested form:
# At the file of the nested model

class Product::Image
  default_scope order :row_order # this will be picked up by RA to order
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment