我的模型"combobox" has_many "comboboxselects"和"comboboxselects" belongs_to "combobox"。"comboboxes“的Activescaffold在comboboxselects column中显示数据,就像"#<Comboboxselect:0x472d25c>"。如何显示“comboxselect”表中的"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发布于 2009-05-18 23:22:39
首先,config.list.columns中引用的所有字段都必须包含在config.columns中(任何显式定义的config.*.columns字段都必须是config.columns的子集)。
然后,在每个还没有name、title字段或方法的模型中,您必须声明此自定义方法:
class Comboboxselect < ActiveRecord::Base
belongs_to :combobox
def to_label
"#{answer}"
end
end请参阅ActiveScaffold文档:Describing Records: to_label
发布于 2009-05-18 09:31:25
当您说显示时,我假设您是指在视图中?你能在你运行的时候发布代码以获得输出吗?
在我看来,你只是有了Comboboxselect对象,你有没有试过向它添加.answer来访问你想要的属性?
https://stackoverflow.com/questions/874329
复制相似问题