我有一张表格
<% 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 :(得分: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})