2

我正在尝试使用 aform_for collection_select来显示帐户类型的一些选择字段选项。

我突然想到,如果用户可以在每个选择选项中看到该类型的价格,他们会更容易

这是我目前不工作的代码:

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

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

4

1 回答 1

2

请参阅文档: 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 %>
于 2013-05-03T12:25:14.597 回答