gpt4 book ai didi

ruby-on-rails - ActiveScaffold - 更改关联对象的默认名称

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:06 25 4
gpt4 key购买 nike

我的模型 "combobox" has_many "comboboxselects" , 和 "comboboxselects" belongs_to "combobox" . “组合框”的事件支架在组合框选择列中显示数据,如 "#<Comboboxselect:0x472d25c>" .如何显示表“comboxselects”中的“answer”列?

模型:

class Combobox < ActiveRecord::Base
has_many :comboboxselects
end

class Comboboxselect < ActiveRecord::Base
belongs_to :combobox
end

架构:

  create_table "comboboxes", :force => true do |t|
t.string "question"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "comboboxselects", :force => true do |t|
t.integer "combobox_id"
t.string "answer"
t.datetime "created_at"
t.datetime "updated_at"
end

输出:

class ComboboxesController < ApplicationController
active_scaffold :combobox do |config|
config.list.columns = [:id, :question]
config.columns = [:question, :comboboxselects]
end
end

class ComboboxselectsController < ApplicationController
active_scaffold :comboboxselect do |config|
config.list.columns = [:id, :combobox, :answer]
config.columns = [:answer]
end
end

最佳答案

首先,config.list.columns 中引用的所有字段都必须包含在 config.columns 中(任何显式定义的 config.*.columns 字段必须是 config.columns 的子集)。

然后,在每个还没有名称或标题字段或方法的模型中,您必须声明此自定义方法:

class Comboboxselect < ActiveRecord::Base
belongs_to :combobox
def to_label
"#{answer}"
end
end

请参阅 ActiveScaffold 文档:Describing Records: to_label

关于ruby-on-rails - ActiveScaffold - 更改关联对象的默认名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/874329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com