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

发布于 2024-07-21 07:04:36 字数 1210 浏览 2 评论 0原文

我的模型“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2024-07-28 07:04:36

首先,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

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:

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

See ActiveScaffold documentation: Describing Records: to_label

腻橙味 2024-07-28 07:04:36

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

在我看来,您只有 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文