daniel barrera
daniel barrera

Reputation: 367

Create a select field in Ruby On Rails

I have a controller called user with the action new. This action has a view associated called 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

The problem is I want to create a select field in ROR for fill this with the array of provinces @provinces

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

Upvotes: 0

Views: 215

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

Despite the fact, that I have no idea what this line @provinces=provinces.all does (I assume, you've meant Province.all, but that's just a suggestion), the select would look as follows:

<%= 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.

Upvotes: 2

Related Questions