Rails 4 simple_form collection_select

markhorrocks picture markhorrocks · Jul 17, 2013 · Viewed 51k times · Source

How do I translate the following to simple form?

<%= form_for(@bill) do |f| %>

<%= f.label :location_id %>
<%= f.collection_select(:location_id, @locations, :id, :location_name, 
      {:selected => @bill.location_id}) %>

I can't use a simple association because @locations is the result of a where query.

Answer

Viren picture Viren · Jul 19, 2013

Try this

<%= simple_form_for @bill do |f| %>
   <%= f.input :location,:collection => @locations,:label_method => :location_name,:value_method => :id,:label => "Location" ,:include_blank => false %>
   <%= f.button :submit %>
<%end%>

Hope this help