Manmeet
Manmeet

Reputation: 27

Make the default option of select tag to be of current value of name in Rails

This piece of code is in my edit user attributes page. I have a subscription_type:string attribute for my model User in my rails app. The default value of the attribute is "". I want the select tag to show the option to be of the current value of subscription_type ("", Silver, Gold or Platinum) for the particular user. How can I achieve this?

<select class="form-group" name = "user[subscription_type]">
   <option value="">Select your subscription type</option>
   <option value="Silver">Silver</option>
   <option value="Gold">Gold</option>
   <option value="Platinum">Platinum</option>
</select>

Upvotes: 1

Views: 124

Answers (1)

ABMagil
ABMagil

Reputation: 5675

You're going to want to use the select_tag and the options_for_select methods and pass it an array of arrays- something like

options_for_select([['Gold', 'Gold'], ['Silver', 'Silver'],['Platinum', 'Platinum']], @user.subscription_type)

Upvotes: 4

Related Questions