如何在Rails表单中获取所选下拉项的索引

时间:2014-09-16 14:35:41

标签: ruby-on-rails forms

如何获取用户从表单字段中选择的下拉列表选择的索引号?我希望能够将此索引号保存到Rails数据库。

= simple_form_for(Rule.new, html: {:multipart => true, autocomplete: "off", :class=> "form-horizontal" }, remote: true, :authenticity_token => true) do |f|
    = f.select :header, @headers.each{|header|}, {:prompt   => "Select a Header"}, {id: "ruleHeader_select"}
    br
    = f.collection_select(:rule_type_id,  @ruleTypes, :id, :name, {:prompt   => "Select a Rule Type"}, {:id => 'ruleTypes_select', style: "display:none;"})
    = f.button :submit

1 个答案:

答案 0 :(得分:1)

您可以将数组数组作为选项传递到collection_select

@ruleTypes.each_with_index.to_a

这将显示从@ruleTypes到用户的值,而选定的值将是索引。