Krawalla
Krawalla

Reputation: 198

How to reject "wrong" inputs in a from in Ruby on Rails

I have a form linked to a table. One column of the table is "smoker". I need either a "1" or a "0" in that column. To do this, I created another cloumn as a dummy called "dummysmoker".

The dummysmoker column will be filled in by a user and it's set to assign the corresponding "smoker" column a "1" if a user answers "yes" (and puts yes in the dummysmoker column).

While a "1" is only generated if the user puts "yes" in the dummy column, a "0" is generated in any other case (e.g. "no", "not", "hell, no!", ...).

However, I would like to limit the user to only being able to select "yes" or "no" (maybe through a radio button tag?).

The problem is, I don't know how to include suggested data entries in a radio button tag, I only figured a way to link them to existing table entries.

Does anyone have suggestions?

Upvotes: 0

Views: 121

Answers (1)

uday
uday

Reputation: 8710

There are couple of ways to achieve what you want:

  1. You can use radio button or
  2. Dropdown select

Using a radio button:

<% form_for(@myform) do |f| %>
  <%= f.radio_button :smoker, '1' %> 
  <%= f.label :smoker, 'Yes', :value => '1' %>
  <%= f.radio_button :smoker, '0' %>
  <%= f.label :smoker, 'No', :value => '0' %>
<% end %>

Using dropdown:

<%= select_tag(:smoker, options_for_select([['Yes', 1], ['No', 0]])) %>

Upvotes: 2

Related Questions