2009-06-27 56 views
6

我有一個FinancialDocument#document_type模型屬性。我想,讓用戶選擇從字符串數組填充一個HTML選擇菜單中的文件類型...在Rails中,如何使用字符串數組實現HTML選擇菜單?

doctypes = [ 'Invoice', 'Packing slip', 'Other' ] 

對於每個選項,所顯示的標籤和返回值將是相同的。

我看着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 

我得到這個錯誤...

undefined method `merge' for :to_s:Symbol 

是否有不同的幫手,我可以用這個?或者使用selectcollection_select

回答

11

doctypes一個ActiveRecord集合?看看代碼似乎並不如此。 您可以使用select幫手。

= f.select :document_type, doctypes, :include_blank => true 

而且,你不需要通過@financial_document如果調用的form_for創建的窗體對象上的標籤。

1
doctypes.map!{|d| [d]} 
f.select(@financial_document, :document_type, doctypes) 

會這樣做我想。

+0

謝謝,但沒有工作對我來說...未定義的方法'合併」的[[ 「發票」],[ 「PS」],[ 「裝箱單」],[ 「其他」]:數組 – Ethan 2009-06-27 18:07:43