Rails 4 simple_form collection_select

mar*_*cks 26 ruby-on-rails simple-form

如何将以下内容翻译成简单的表格?

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

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

我不能使用简单的关联,因为@locations是where查询的结果.

Vir*_*ren 60

试试这个

<%= 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%>
Run Code Online (Sandbox Code Playgroud)

希望这有帮助


mar*_*cks 7

这是决赛:

<%= f.input :location_id, collection: @locations, label_method: :location_name, value_method: :id,label: "Location", include_blank: false, selected: @bill.location_id %>
Run Code Online (Sandbox Code Playgroud)