Rails 4 simple_form collection_select

∥☆過路亽.° 提交于 2019-11-30 02:59:00

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

Here is the final:

<%= f.input :location_id, collection: @locations, label_method: :location_name, value_method: :id,label: "Location", include_blank: false, selected: @bill.location_id %>

In simple form, if we have an association between location and bill, than we can do like this:

<%= simple_form_for @bill do |f| %>
  <%= f.association :location, collection: @locations, priority: @bill.location %>
  <%= f.button :submit %>
<% end %>

Hope this will help.

In your location model, you can also create a :

def to_label
  location_name
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!