Search code examples
ruby-on-railssimple-formselected

Simple_form (rails) - How can I set default selected option if I have params[:id]


I'm trying to set value in my simple_form association field:

<%= f.association :names %>

Now in this field I have some values, for example: John, Jack, Jerry.

How can I set, for example, default value Jerry if I have params[:name] = Jerry?

I tried this:

<%= f.association :names, selected: params[:name] %>
<%= f.association :names, input_html: { value: params[:name] } %> 

But it doesn't work.


Solution

  • Try to use this code to set default value:

    <%= f.association :names, default: params[:name] %>
    

    See this link