2

我的模型"combobox" has_many "comboboxselects",和"comboboxselects" belongs_to "combobox"。“组合框”的 Activescaffold 在组合框选择列中显示数据,如"#<Comboboxselect:0x472d25c>". 如何从表“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
4

2 回答 2

1

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

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

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

请参阅 ActiveScaffold 文档:描述记录:to_label

于 2009-05-18T23:22:39.183 回答
0

当您说显示时,我假设您的意思是在视图中?您可以发布您运行的代码以获得该输出。

在我看来,您只有 Comboboxselect 对象,您是否尝试添加.answer到它以访问您想要的属性?

于 2009-05-18T09:31:24.693 回答