Rails select method with default value

When using the select form builder method in rails, for instance:

f.select

In conjunction with the options_from_collection_for_select method, like

f.select :field, options_from_collection_for_select(@fields, ‘id’, ‘name’)

Remember, there is a fourth argument in the above method, which defines the selected value, like so

f.select :field, options_from_collection_for_select(@fields, ‘id’, ‘name’, @object.field)

1 thought on “Rails select method with default value

Leave a Reply

Your email address will not be published. Required fields are marked *