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| %>

<%= radio_button_tag 'exp', 2 %>2 <%= radio_button_tag 'exp', 1 %>1 <%= link_to 'Submit now', {:action=>"destroy"},:remote => true, :submit => 'radio_buttons' %>
<%end%>

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

Kindly give me the solution

On Sun, Dec 4, 2011 at 7:38 PM, Guru R. [email protected] wrote:

‘radio_buttons’ %>

<%end%>

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’


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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.

On Sun, Dec 4, 2011 at 6:19 AM, Guru R. [email protected] wrote:

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

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).

HTH!

Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan

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


  <br>

  <input type="radio" name="radios1" 

onclick=this.parentNode.submit() value="<%[email protected]%>"><%[email protected]%>
<%[email protected]%>
<%[email protected]%>
<%[email protected]%>

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

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| %>

<%=f.radio_button:e,@abc.a%> <%=f.label:rating_1,@abc.a%> <%=f.radio_button:e,@abc.b%> <%=f.label:rating_1,@abc.b%> <%=f.radio_button:e,@abc.c%> <%=f.label:rating_1,@abc.c%> <%=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.

On Tue, Dec 6, 2011 at 8:15 PM, Guru R. [email protected] wrote:

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.


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan

On Tue, Dec 6, 2011 at 3:58 AM, Guru R. [email protected] wrote:

<%[email protected]%>

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.

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?


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs