Reputation:
I have a simple_form collection which contains a list of languages. I want to select 'German' by default, but the selected: option in simple_form requires an id. I could obtain the id of 'German' but would hope that wasn't necessary.
= f.association :language, selected: // not sure what to put here
This works, but stinks (I will NOT be using such atrocious code):
= f.association :language, selected: Language.where("name = 'German'").first.id
I would hope for something like:
= f.association :language, selected: { |lan| lan.name == 'German' }
Every example I've found during the last hour involves the id. Not one example of how to select via the name.
Upvotes: 1
Views: 1013
Reputation: 24637
Yeah, SimpleForm
can accept proc for selected
option. In your case the code is:
= f.association :language, selected: lambda { |lan| lan.name == 'German' }
Upvotes: 6