首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActiveScaffold -更改关联对象的默认名称

ActiveScaffold -更改关联对象的默认名称
EN

Stack Overflow用户
提问于 2009-05-17 10:33:15
回答 2查看 1.4K关注 0票数 2

我的模型"combobox" has_many "comboboxselects""comboboxselects" belongs_to "combobox"。"comboboxes“的Activescaffold在comboboxselects column中显示数据,就像"#<Comboboxselect:0x472d25c>"。如何显示“comboxselect”表中的"answer“列?

型号:

代码语言:javascript
复制
class Combobox < ActiveRecord::Base
 has_many :comboboxselects
end

class Comboboxselect < ActiveRecord::Base
 belongs_to :combobox
end

模式:

代码语言:javascript
复制
  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

输出:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-05-18 23:22:39

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

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

代码语言:javascript
复制
class Comboboxselect < ActiveRecord::Base
 belongs_to :combobox
 def to_label
  "#{answer}" 
 end
end

请参阅ActiveScaffold文档:Describing Records: to_label

票数 1
EN

Stack Overflow用户

发布于 2009-05-18 09:31:25

当您说显示时,我假设您是指在视图中?你能在你运行的时候发布代码以获得输出吗?

在我看来,你只是有了Comboboxselect对象,你有没有试过向它添加.answer来访问你想要的属性?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/874329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档