如何在选择框中获取查找表值?
与敏捷书一起阅读,它在脚注中这样说:
...您只需将查找时执行 find(:all) 的结果传递给 select 助手 桌子。
好吧...
<%= f.select :source, Source.find(:all) %>
我的源控制器(因此表格)看起来像这样:
create_table :sources do |t|
t.string :source
t.timestamps
end
但是我的选择框一团糟,我在所有值和显示中都得到了这种类型的数据:
#<Source:0x23a2bfc>
所以我尝试做一个 Source.find(: all, :select => 'name,id')
但这仍然给了我看起来很奇怪的东西。
我做错了什么?
Reading along with the agile book, it says this in a footnote:
... You simply pass the select helper the result of doing a find(:all) on your lookup
table.
Okay ...
<%= f.select :source, Source.find(:all) %>
My source controller (and therefore table) looks like this:
create_table :sources do |t|
t.string :source
t.timestamps
end
But my select box is a mess, I get this type of data in all the values and displays:
#<Source:0x23a2bfc>
So I tried doing a Source.find(:all, :select => 'name,id')
but that still gives me the whacky looking stuff.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将返回 Source 对象的数组 - 这不是你想要的。您想要的是一系列选项。类似的东西:
will return an array of Source objects - this is not what you want. What you wants is an array of options. Something like:
您需要传递选项集合(或键值选项)。
select
中的第三个参数实际上传递给options_for_select
方法。最好用一个例子来说明这一点。... 变为
大多数时候您会对键值对更感兴趣
... 变为
作为一种简写方式,传递整个对象集合并将它们映射以符合
options_for_select正在等待。
...并使用视图内的
select
方法回答您的问题You need to pass a collection of options (or key-value options). The third parameter in
select
is actually passed to theoptions_for_select
method. This is best illustrated with an example.... becomes
Most of the time you'll be more interested in key-value pairs
... becomes
As a short-hand, it's common to pass an entire collection of objects and map them to conform what
options_for_select
is expecting....and to answer your question using the
select
method inside of a view