如何在 Rails 管理关联模型中制作自定义下拉菜单
How to make customisation drop-down in Rails Admin associations model
Rails 管理员 has_many 关联选项下拉自定义
我想在 rails 管理 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
首页post.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 下拉菜单中显示 post 图片 URL,ID 为 post。我如何在 rails 管理员中执行此操作?
点赞Post #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
首先在 rails 管理初始值设定项上进行配置,通常在配置块内的 config/initializers/rails_admin.rb
上进行配置,该方法将在您的模型中用于确定要为每个 object 显示的标题像这样:
RailsAdmin.config do |config|
config.label_methods = [:rails_admin_title]
end
然后在您的 post 模型上定义该方法
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 管理员 has_many 关联选项下拉自定义
我想在 rails 管理 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
首页post.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 下拉菜单中显示 post 图片 URL,ID 为 post。我如何在 rails 管理员中执行此操作? 点赞Post #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
首先在 rails 管理初始值设定项上进行配置,通常在配置块内的 config/initializers/rails_admin.rb
上进行配置,该方法将在您的模型中用于确定要为每个 object 显示的标题像这样:
RailsAdmin.config do |config|
config.label_methods = [:rails_admin_title]
end
然后在您的 post 模型上定义该方法
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
您可能需要重新启动开发服务器才能看到此更改。