1

Form:

<%= form_for :computer do |f| %>
  <%= f.radio_button :state, value: 'on' %>
  <%= f.radio_button :state, value: 'off' %>
<% end %>

Outputs:

params[:computer][:state]
{:value=>"on"}

params[:computer][:state].class
String

params[:computer][:state][:value]
No implicit conversion of string into integer

params[:computer][:state]['value']
value

No, there is no .to_s anywhere! :)

There is this question, but it's been unanswered since 2011 and is for a different version of rails.

user8897013
  • 443
  • 4
  • 15

1 Answers1

0

The proper usage of radio_button in your case is the following:

f.radio_button :state, 'on'

The second argument is the value to pass when selected, you don't need the key/value pair value: 'on' there.

It will end up mapping the value in params[:computer][:state]

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • omg, [wrong version](https://stackoverflow.com/questions/15060268/how-to-use-radio-button-correctly-in-rails)! i'm guessing this is going to become more and more of a problem for the stupid like me as the years pass and pages accumulate. – user8897013 Jan 03 '19 at 22:14
  • it is good to always take a look at https://api.rubyonrails.org/, append at the end of the url the Rails version you're working on like `v5.0.7/`, in this case https://api.rubyonrails.org/v5.0.7/, and you'll get the documentation for that version. There you can see the syntax you should be using. – fanta Jan 03 '19 at 23:37