How to set default value for association with simple_form?

749 views Asked by At

I'd like to do something like this:

<%= f.association :productgroup, default: params[:productgroup_id] %>

The params should only be used if there is no value (lets say for new records), so selected: is not what I'm looking for because it overrides the value when I edit the record.

Thanks, Andreas

1

There are 1 answers

0
flashharry82 On BEST ANSWER

Set the value in your controller for the form object eg

controller

def show
    @product = Product.new();
    @product.product_group = ProductGroup.find(1) // set default
end

view

simple_form_for @product do |f|
    f.association :product_group