在Rails中,如何使用字符串数组实现HTML选择菜单?

Eth*_*han 6 ruby ruby-on-rails

我有一个FinancialDocument#document_type模型属性.我想让用户从一个字符串数组填充的HTML选择菜单中选择文档类型...

doctypes = [ 'Invoice', 'Packing slip', 'Other' ]
Run Code Online (Sandbox Code Playgroud)

对于每个选项,显示的标签和返回值将是相同的.

我看着selectcollection_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
Run Code Online (Sandbox Code Playgroud)

我收到这个错误......

undefined method `merge' for :to_s:Symbol
Run Code Online (Sandbox Code Playgroud)

我可以使用不同的助手吗?还是一种使用方式selectcollection_select

Sim*_*tti 11

doctypes是ActiveRecord集合吗?看看代码似乎并非如此.你可以使用select帮助器.

= f.select :document_type, doctypes, :include_blank => true
Run Code Online (Sandbox Code Playgroud)

此外,@financial_document如果在使用form_for创建的表单对象上调用标记,则无需传递.