ActiveScaffold - 更改关联对象的默认名称
我的模型“combobox”有_许多“comboboxselects”
,并且“comboboxselects”属于_to“combobox”
。 “comboboxes”的 Activescaffold 在组合框选择列中显示数据,如 "#
。 如何显示表“comboxselects”中的“答案”列?
型号:
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
My model "combobox" has_many "comboboxselects"
, and "comboboxselects" belongs_to "combobox"
. Activescaffold of "comboboxes" displays data in comboboxselects-column like "#<Comboboxselect:0x472d25c>"
. How to make display the "answer" column from table "comboxselects"?
Models:
class Combobox < ActiveRecord::Base
has_many :comboboxselects
end
class Comboboxselect < ActiveRecord::Base
belongs_to :combobox
end
Schema:
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
Output:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,config.list.columns 中引用的所有字段都必须包含在 config.columns 中(任何显式定义的 config.*.columns 字段必须是 config.columns 的子集)。
然后,在每个还没有名称或标题字段或方法的模型中,您必须声明此自定义方法:
请参阅 ActiveScaffold 文档:描述记录:to_label
First, all fields referenced in config.list.columns have to be included in config.columns (any explicitly-defined config.*.columns fields must be subsets of config.columns).
Then, in each model that does not already have a name or title field or method, you have to declare this custom method:
See ActiveScaffold documentation: Describing Records: to_label
当你说显示时,我假设你的意思是在视图中? 您可以发布您运行的代码以获得该输出吗?
在我看来,您只有 Comboboxselect 对象,您是否尝试向其中添加
.answer
来访问您想要的属性?When you say displays I assume you mean in a view? Can you post the code your running to get that output.
Looks to me like you just have Comboboxselect object, have you tried adding
.answer
to it to access the attribute you want?