Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails radio buttons - one choice for multiple columns in on model

I want user to choose one option out of three for one model.

I.e. I have a model Video, that can be rated as positive/negative/unknown

Currently I have three columns with boolean values (pos/neg/unknown).

Is it the best way to handle this situation?

What should the form look like for this?

Currently I have something like

<%= radio_button_tag :positive, @word.positive, false %> 
<%= label_tag :positive, 'Positive' %>
<%= radio_button_tag :negative, @word.negative, false %> 
<%= label_tag :negative, 'Positive' %>
<%= radio_button_tag :unknown, @word.unknown, false %> 
<%= label_tag :unknown, 'Positive' %>

But obviously it allows multiple selections, while I am trying to restrict it to just one..

What to do?

like image 663
Stpn Avatar asked Dec 06 '11 18:12

Stpn


1 Answers

If would go with a string column let's say rating.

then in your form:

# ...
<%= f.radio_button :rating, 'unknown', checked: true %>
<%= f.radio_button :rating, 'positive' %>
<%= f.radio_button :rating, 'negative' %>
# ...

It only allows one selection

edit The exact same but using radio_button_tag:

<%= radio_button_tag 'rating', 'unknown', true %>
<%= radio_button_tag 'rating', 'positive' %>
<%= radio_button_tag 'rating', 'negative' %>
like image 66
Damien Avatar answered Oct 03 '22 15:10

Damien