如何获取options_from_collection_for_select的多个字段 [英] How to get multiple fields for options_from_collection_for_select

查看:33
本文介绍了如何获取options_from_collection_for_select的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 select_tag 中有以下内容.它工作正常.(我使用 select_tag 是因为它用于与模型无关的搜索.)

I have the following in a select_tag. It works fine. (I'm using select_tag because it is for a search not tied to a model.)

options_from_collection_for_select(@customers, :id, :first_name)

当前的 HTML 输出为:

The current HTML output is:

<option value="4">Fred</option>

但我想要:

<option value="4">Fred Flintstone</option>

我想显示全名而不是只显示名字.我似乎无法让它同时使用first_name"和last_name"这两个字段,也无法弄清楚如何让它调用连接两个字段的方法.我怎样才能让它工作?

I want to display the full name instead of just the first name. I can't seem to get it to use both fields "first_name" and "last_name" nor can I figure out how to get it to call a method where I concatenate the two fields. How can I get this to work?

推荐答案

您可以在模型上定义:

定义名称;"#{first_name} #{last_name}";end

并使用:

options_from_collection_for_select(@customers, :id, :name)

这篇关于如何获取options_from_collection_for_select的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆