【问题标题】:How to make customisation drop-down in Rails Admin associations model如何在 Rails 管理员关联模型中进行自定义下拉
【发布时间】:2023-03-10 04:55:01
【问题描述】:

Rails Admin has_many 关联选项删除自定义

我想在 rails admin select 下拉选项中进行一些更改。我有一些具有 has_many 关系的模型。

模型文件 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 管理员配置 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

现在我想在 Post 下拉菜单中显示带有帖子 ID 的帖子图片 URL。我如何在 rails admin 中做到这一点? 喜欢发布 #1370 post_test.png

架构

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

【问题讨论】:

    标签: ruby-on-rails-5 rails-admin


    【解决方案1】:

    首先在 Rails 管理员初始化程序上配置,通常在配置块内的 config/initializers/rails_admin.rb 上配置一个方法,该方法将在您的模型中用于确定要为每个对象显示的标题,如下所示:

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

    然后在您的帖子模型上定义该方法

    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
    

    您可能需要重新启动开发服务器才能看到此更改。

    【讨论】:

    • 感谢您的回复。你有没有看到帖子有很多照片的关系?它将如何在 rails_admin_title 中工作?
    • 当然可以,每个帖子实例都可以访问相关的photos.first。我更新了答案以包含完整模型以避免混淆。
    • 如何显示图片或为图片添加链接?我需要为用户单击链接并在新选项卡中打开或在列表选项中显示图像的图像添加链接。
    • 那是另一个 stackoverflow 问题,我强烈建议您创建它,我很乐意在那里回答。否则我们会破坏这一切。
    • 当然,类似于新的 rails_admin_title 方法,您可以通过将 .html_safe 方法添加到返回字符串来将任何字符串呈现为 html
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2017-02-08
    • 2014-07-11
    • 2018-10-02
    相关资源
    最近更新 更多