-1

In simple_form, you can do use label_method and value_method, like the below..

f.association :company, collection: Company.all.order(:name), label_method: :name, value_method: :value

1) Does form_for have the same methods?

2) What are the default values for :label_method and :value_method in SimpleForm?

Solution (set in initializer):

config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
config.collection_value_methods = [ :id, :to_s ]
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

0

Yes, default FormBuilder has same abilities:

<%= form_for @yourmodel do |f| %>
  <%= f.collection_select :company_id, Company.all.order(:name), :id, :name %>
  <%= f.submit %>
<% end %>

According to collection_select helper documentation

collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) public

# File actionview/lib/action_view/helpers/form_options_helper.rb, line 834
def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
    @template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options))
end
cnnr
  • 1,267
  • 4
  • 18
  • 23