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

How to make customisation drop-down in Rails Admin associations model


Rails Admin has_many associations option drop customization

I want to make some changes in rails admin select drop-down option. I have some models with has_many relationship.

Model Files post.rb

class Post < ApplicationRecord
    has_many :photos, dependent: :destroy
    accepts_nested_attributes_for :photos, allow_destroy: true
    def post_image
       photos&.first&.asset_url
    end
end

photo.rb

class Photo < ApplicationRecord
  belongs_to :post
end

homepage.rb

class Homepage < ApplicationRecord
    has_many :homepagepost
    has_many :posts,-> { order(likes_count: :desc) }, through: :homepagepost
end

homepagepost.rb

class Homepagepost < ApplicationRecord
    belongs_to :post
end

Rails admin config rails_admin.rb

config.model Homepage do
    label "Homepage Rows"
    edit do
      field :title_heading
      field :posts
      field :status
    end
    list do
      field :title_heading
      field :posts
      field :status
      field :created_at
      field :updated_at
    end
end

Now I want to show post image URL in Post drop-down with the post ID. how I do this in rails admin? Like Post #1370 post_test.png

enter image description here

Schema

create_table "photos", id: :serial, force: :cascade do |t|
    t.string "asset_url"
    t.integer "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["post_id"], name: "index_photos_on_post_id"
end

create_table "posts", id: :serial, force: :cascade do |t|
    t.integer "user_id"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_posts_on_user_id"
end 

create_table "homepageposts", force: :cascade do |t|
    t.integer "homepage_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "post_id"
    t.index ["post_id"], name: "index_homepageposts_on_post_id"
end

create_table "homepages", force: :cascade do |t|
    t.string "title_heading"
    t.integer "status"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

Solution

  • First configure on the rails admin initializer, typically on config/initializers/rails_admin.rb inside the configuration block a method that will be used in your models to determine the title to show for each object like this:

    RailsAdmin.config do |config|
      config.label_methods = [:rails_admin_title]
    end
    

    Then on your post model define that method

    class Post < ApplicationRecord
        has_many :photos, dependent: :destroy
        accepts_nested_attributes_for :photos, allow_destroy: true
    
        def rails_admin_title
          "<a href='#{post_image}'>Link</a>".html_safe
        end
    
        def post_image
          photos&.first&.asset_url
        end
    end
    

    You might need to restart your development server to see this change.