这段代码在我的编辑用户属性页面中。我的 Rails 应用程序中的模型用户有一个 subscription_type:string 属性。该属性的默认值为“”。我希望 select 标签显示特定用户的 subscription_type(“”、Silver、Gold 或 Platinum)当前值的选项。我怎样才能做到这一点?
<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>
最佳答案
您将要使用 select_tag和 options_for_select方法并将它传递给一个数组数组——类似于
options_for_select([['Gold', 'Gold'], ['Silver', 'Silver'],['Platinum', 'Platinum']], @user.subscription_type)
关于html - 将 select 标签的默认选项设为 Rails 中 name 的当前值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23896529/