0

Using SimpleForm, Can I set the default values for :label_method and :value_method so I do not need to set it for each input?

By default, I mean a place to set label_method and value_method for all my inputs, so I do not need to set them for each input.

Example:

Instead of this:

<%= f.association :model_in_question, include_blank: false, label_method: :label_for_form, value_method: :value_for_form %>

I want this:

<%= f.association :model_in_question, include_blank: false %>
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

0

I think this is what you're looking for:

Simple form association custom label name

<%= f.association :owner_type, :include_blank => false, :label_method => lambda { |owner| "#{owner.name} | #{owner.subtype_name}" } %>

The same logic applies for value_method. So if you have a model, you can make a method called label_for_form and value_for_form which return your required values. Then in your form:

<%= f.association :model_in_question, include_blank: false, label_method: :label_for_form, value_method: :value_for_form %>

So long as the objects in your collection respond to both those methods, then you won't need to manually write out the collection in the form.

Mark
  • 6,112
  • 4
  • 21
  • 46