2011-05-03 120 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