2011-05-03 79 views
0

我有一個表格Ruby on Rails:如何使用select?

<% form_for(@xyz) do |f| %> 
    <%= f.error_messages %> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 


     <%= f.label :description %> 
     <%= f.text_field :description %> 

     <%= f.label :type %> 
    <%= f.select :type, XYZ::ALL_TYPES %> 
    <% end %> 

我添加屬性「metric_type」這種形式,我想使用它,但此表的。 我在模型文件恆定 AXY = 「AXY」 BXY = 「BXY」 CXY = 「CXY」 ALL_METRICS_TYPE = [AXY,BXY,CXY]

如何使用上面作爲一個下拉

度量類型:

感謝,

回答

0

我建議你使用以下方法一般:

1)在視圖:

<%= your_form_variable.select(: type, Your_model:: ALL_METRICS_TYPE, {:include_blank => 'Please select'}) %> 

2)在模型:

ALL_METRICS_TYPE = [["value which will passed and stored in db", 'what you want to display in the dropdown'], [...]] 

編輯:

要使用它的形狀以外,可以使用這裏所描述的原料選擇助手:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

select("foo", "bar", Your_model:: ALL_METRICS_TYPE, {:include_blank => true}) 
+0

謝謝,我知道這一點,但我有一個不同的情況。我需要在form_for範圍後使用<% select %>,我會在<% end% >後面說。 – user659068 2011-05-03 20:22:50

+0

什麼是foo和酒吧在這裏...? – user659068 2011-05-03 22:23:50

+0

:)看看鏈接,他們會創建你選擇的html標籤的名稱 – apneadiving 2011-05-03 22:38:25