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 %>
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.