Rails, simple_form, how to update switches with the selected option after saving?

I am new to rails, and this is also my first post, but I am trying to create simple_form on a watch page, which should be a settings page for some user, and I have been looking for 2 days already. for an answer, and I can’t find anything.

<%= f.input :gender, label: 'What is your gender?', collection: [ ['M', 'Male' ], ['F', 'Female'] ], as: :radio_buttons, label_method: :last, value_method: :first, checked: 'M', item_wrapper_class: 'inline'%>

This is my input label, and the only option so far is to install: checked => 'M', so after saving the user settings the switch will be selected (but this will not be correct if I select the "Female" gender, because after saving the page By default, the “Male” switch will be selected).

So, is there a way to use the radio buttons in a simple way (or even in a drop-down list) so that after the page is saved, the gender remains selected?

+5
source share
1 answer

Try using the user's gender attribute instead of the marked "M" fo symbol:

<%= f.input :gender, label: 'What is your gender?', collection: [ ['M', 'Male' ], ['F', 'Female'] ], as: :radio_buttons, label_method: :last, value_method: :first, checked: @user.gender, item_wrapper_class: 'inline'%>
+16
source

All Articles