Using the simple_form gem, I've a form with the following field:
f.association :company, label_method: :company_name, value_method: :id, include_blank: false
How can I sort the resulting array by a attribute, like :name? The docs only mention using order for another case, using collection: .... Just adding order: :name to my input doesn't work.
Is this possible at all? Thanks!
You can specify the ordering of the collection items like this:
f.association :company, collection: Company.all.order(:name), label_method: :name, value_method: :id
I don't think there is another way to sort the displayed items, you have to use collection.
Related
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 %>
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.
I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).
I have some properties on an active admin model, which can have a LOT of different values, so right now I'm displaying them using checkboxes:
PropertyType.find_each do |pt|
f.input :property_values
f.input :property_values, label: pt.display_name, as: :check_boxes, collection: pt.property_values.order(name: :asc, display_name: :asc).load , multiple: true
end
What I would like to do is to add an input field, in which while i'm writing, it filters the whole checkboxes list, only displaying the ones that matches the input field.
Is there a way to do that?
Thanks.
Yes. Check out chosen_rails gem.
In active_admin it will look something like this:
f.input :property_values,
label: pt.display_name,
as: :check_boxes,
collection: pt.property_values.order(name: :asc, display_name: :asc).load,
input_html: { class: 'chosen-select' },
multiple: true
The only thing I haven't tried it with check_boxes, but with as: :select, and it works perfectly. I think select would be doing the same for you, since you have multiple: true
I have a drop down select that is set up like below:
<%= select_tag :city_id, option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name) %>
It works fine, except that when I load the edit view the list loads the first item in the select, not the saved value. Is there parameter I'm missing? On rails 4.
According to the documentation on option_groups_from_collection_for_select found here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select
It has a sixth parameter that is the selected value, so just add the last parameter with the value you want and it will work:
<%= select_tag :city_id,
option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name, "your_city") %>
Instead of using select_tag use select
# f being your form object
<%= f.select :city, option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name) %>
Considering you have a valid association with city
I am using simple form gem and I am wondering how do you do a collection_select ? I know you can do a select, but how do you get the values from a collection?
So, for example, how would you do this in a simple form:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)
Thanks
You would do it like this:
collection_select(:post, :author, Author.all, :id, :name_with_initial)
Take a look at SimpleForm - Collection Select Input