在Ruby On Rails中创建一个选择字段

时间:2014-12-07 23:18:52

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我有一个名为user的控制器,其操作为new。此操作的关联视图名为new.html.erb

 def new
    @break_point = BreakPoint.new
    @provinces=Province.all

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @break_point }
    end
  end

问题是我想在ROR中创建一个选择字段,用省份@provinces填充这个

Example  <% @provinces.each do |province| %>
            <% f.select ?    %>
         <% end %>

1 个答案:

答案 0 :(得分:2)

尽管如此,我不知道这句话@provinces=provinces.all做了什么(我假设,你的意思是Province.all,但这只是一个建议),选择看起来如下:

<%= f.select :province, collection: provinces.map(&:name) %> #where `name` is an attribute of Province model, and you can substitute it with whatever attribute you want user to be able to select by.