Match an option in select form with boolean values

拥有回忆 提交于 2019-12-03 09:55:32

You can provide a pair of values for each options: first will be used as label (inner text of <option> tag), second will be used as a value attribute:

= f.select :active, [['Active', true], ['Inactive', false]]

It'll render something like:

<select name="model[active]">
  <option value="true">Active</option>
  <option value="false">Inactive</option>
</select>

Have a look at the docs for select and options_for_select.

A small extension of the earlier answer, if you're using a dropdown.

I needed to use "options_for_select." Also, ":selected" stores the value for the next time you return to the form.

<%= f.select(:active, options_for_select([['Active', true], ['Inactive', false]], {:selected => @symbol.active}),:prompt => "Select") %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!