reading input from radio button

i m trying to read the input of the radio button and compare it with a
string and further increment a count variable as per the result of
comparison.i e used

<%= form_for(@abc) do |f| %>
<div id="radio_buttons">
<%= radio_button_tag 'exp', 2 %>2
  <%= radio_button_tag 'exp', 1 %>1
<%= link_to 'Submit now', {:action=>"destroy"},:remote => true, :submit
'radio_buttons' %>

def destroy
  $score= params[:exp];
  if params[:exp] = '1'
I m not able to see the parameter either $score or @ac

Kindly give me the solution

i m trying to read the input of the radio button and compare it with a

string and further increment a count variable as per the result of

comparison.i e used

<%= form_for(@abc) do |f| %>

<%= radio_button_tag ‘exp’, 2 %>2

<%= radio_button_tag ‘exp’, 1 %>1

<%= link_to ‘Submit now’, {:action=>“destroy”},:remote => true, :submit


‘radio_buttons’ %>


From the api

radio_button_tag(name, value, checked = false, options = {})

the third argument is defaulted to false so your radio buttons start as not checked.

you need to check at least one before the form can submit the value of the radio

button you checked.

You might also want to check your code below.

def destroy

$score= params[:exp];

Someone correct me if I’m wrong but it’s not good practice to prepend a variable with $ in ruby.

if params[:exp] = ‘1’

The line above sets params[:exp] to ‘1’ so @ac below will always be equal to ‘1’

i corrected the "if params[:exp] == '1'".
but i want to save the input got from the radio button to an variable
so that i can compare it with another string and with the result of the
comparison i need to increment a count variable SO so i thought i ve no
other way other than global variable to use it across views and
controllers.But even when i tried the $ way ,@@ way @ way , when I
assign params[:exp] to it it is not getting displayed.Kindly suggest me
a alternative solution.

If this is on a per-user basis, you can save your counter value in the
session; if it's global to the entire app you can use the DB or another
persistent storage (e.g. MongoDB, Redis).


Thanks for ur reply, It was useful. i ve chaged my view in html as


      <input type="radio" name="radios1"
onclick=this.parentNode.submit() value="<%=@abc.a%>"><%=@abc.a%>
    <input type="radio" name="radios1"
onclick="this.parentnode.submit()" value="<%=@abc.b%>"><%=@abc.b%>
     <input type="radio" name="radios1"
onclick="this.parentnode.submit()" value="<%=@abc.c%>"><%=@abc.c%>
<input type="radio" name="radios1" onclick="this.parentnode.submit()"
The code is workin well except that, as soon as a radio button is
selected the the page gets refreshed and the selection is not reflected
in that page after that. i need the selection to be visible even after
the onclick event and also when "history.back" is done.Please do help

So it doesn't sound like it's "working well" to me :slight_smile:

1) Why are you doing this form by hand? I'd recommend you read up
     on Rails forms, including the `radio_button` method.

2) Are you certain that the submit action is saving the input to some
    persistent storage?

Thank for ur reply
i m not sure about how to write the form for this radio_button. if i use
the form_for @abc where abc is the database i m not able to read input
with params[:]. i did use the form as
<%= form_for(@abc) do |f| %>
<tr><%=f.radio_button:e,@abc.a%> <%=f.label:rating_1,@abc.a%></tr>
  <tr><%=f.radio_button:e,@abc.b%> <%=f.label:rating_1,@abc.b%></tr>
  <tr><%=f.radio_button:e,@abc.c%> <%=f.label:rating_1,@abc.c%></tr>
  <%=f.radio_button:e,@abc.d%> <%=f.label:rating_1,@abc.d%>

where the answer selected would get updated to the database. as you said
i don want to update it in the database . i need to modify this form to
a way where i need to read the value of the selection and inmcrement the
counter on per user basis.

This is making less sense as we go: 'abc is the database'?? I totally
don't understand that.

Is the thing represented by 'abc' an object or an attribute of an object?
And if an object, you're saying you don't want it to be an ActiveRecord-
derived object? A more realistic example would help me, at least :slight_smile:

Also, the markup above isn't remotely valid, so you may want to fix
that long the way.