我有一个FinancialDocument#document_type
模型属性。我想让用户从一个字符串数组填充的HTML选择菜单中选择文档类型......
doctypes = [ 'Invoice', 'Packing slip', 'Other' ]
对于每个选项,显示的标签和返回的值都是相同的。
我查看了select
和collection_select
帮助程序,但它们似乎适合选择子模型,而不仅仅是字符串值。我无法发现如何将它们弯曲到我的目的。
以下是我尝试做的事情(我使用的是Haml,而不是Erb)......
form_for(@financial_document) do |f|
- doctypes = [ 'Invoice', 'PS', 'Packing slip', 'Other' ]
= f.collection_select @financial_document, :document_type, \
doctypes, :to_s, :to_s, :include_blank => true
我收到此错误...
undefined method `merge' for :to_s:Symbol
我可以使用不同的助手吗?或者使用select
或collection_select
?
答案 0 :(得分:11)
doctypes是ActiveRecord集合吗?看看代码似乎并非如此。
您可以使用select
帮助程序。
= f.select :document_type, doctypes, :include_blank => true
另外,如果调用使用form_for创建的表单对象上的标记,则无需传递@financial_document
。
答案 1 :(得分:1)
doctypes.map!{|d| [d]}
f.select(@financial_document, :document_type, doctypes)
我会这么做。