【发布时间】: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