Form_for选择字段,将数据连接到显示字符串

问题描述:

我试图使用form_forcollection_select来显示帐户类型的某些选择字段选项。Form_for选择字段,将数据连接到显示字符串

它发生,我认为这将是用户更容易,如果他们能看到的类型的价格在每个选择选项

这是我目前还没有工作的代码:

<%= a.collection_select :account_type, AccountType.all, :id, (:name+" - "+number_to_currency(:price)) %> 

如何我可以连接值,以便(:name+" - "+number_to_currency(:price))将实际工作,而不是抛出一个错误?

+0

http://*.com/a/3838694/ 559119和http://*.com/a/12909620/559119。 – Sun 2013-05-03 08:45:46

参见文档: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

您可以使用:text_method选项设置中选择下拉列表中显示的文本。

在你ACCOUNTTYPE模型,定义这样的方法:

def name_with_price 
    "#{name} - $#{price}" 
    end 

然后,在你看来,你可以使用:

<%= a.collection_select :account_type, nil, AccountType.all, :id, :name_with_price %> 
+0

我在文档中看到过。但那是在模型中显示逻辑,这打破了MVC的原则,对吗? – Kristian 2013-05-03 13:26:00