简体   繁体   中英

Force Rails to assign nil value also using simple_form

I have a use case in a web page, in which I need to have "nil" value also, besides true and false. I have implemented it like bellow but when I choose "unchecked" in GUI it always jumps to 'Yes' (true). Is there anyway to force Rails to assign nil value also ?

Code:

f.input :confirmed, label: 'Confirmed', as: :radio_buttons, collection: [[ true, "Yes" ], [ false, "No" ], [ nil, "unchecked"]], label_method: :second, value_method: :first, selected: [ nil, 'unchecked' ]

You can try use collection_radio_buttons :

<%= f.collection_radio_buttons(:confirmed, [[true, 'Yes'], [false, 'No'], [nil, 'unchecked']], :first, :second,{ checked: [nil, 'unchecked'] }) do |b| %>
  <% b.label do %>
    <%= b.radio_button %> <%= b.text %>
  <% end %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM