getting the id of selected radio button

Hai friends,

I am really new to ROR
I have some radiobuttons...If user select a radiobutton i want to get
the id of the selected radio button to be passed as an id to the index
page of weather controller on clicking the button "view Details"
I don't know how to pass the id? Can you please help me out.


<% for r in @readings %>
   <%= radio_button :contact_preference, "value", :tabindex => %>
<%=h r.station.province %>
<%= button_to "View Details", :controller => "weather", :action =>
"index" %>


Why don’t you try googling a little. All you need here
inside the loop <% for r in @readings %>

is --------------------------
<%= radio_button_tag ‘contact_preference_id’, “#{}”, false, :class=> “classname_if_any”%>

RED is the name of the radio buttons, must be same for all the other radio buttons to facilitate, User must be able to select only one of the radio buttons in the current_readings list.

GREEN is the value of the radio button, i.e. the id of the current_reading. It will be different for every radio button.
BLUE is whether that radio button should be checked or not. But initially you should keep it false only. When user wish to he might select one of them.

GREY is the class variable you want to set to apply some css class to improve the look of the radio button.
#Rest of the code you already have below

And inside the weather_controller
in def index action,

do something like -
#That means someone clicked view Details link.
And you check the value of params[:contact_preference_id], will give you id of the selected radio button.

good luck,

Hai Nitin,
      Thank you once again...But i am not getting the id of the selected
radio button.After selecting and clicking "view details" link,it is
again directing to the same page.(readings/index)


<% for r in @readings %>
   <%= radio_button_tag 'contact_preference_id', "#{}", false %>
<%=h r.station.province %>
<%= link_to "View Details", :controller => "weather", :action =>
"index" %>

Weather Controller
  def index
       station = Station.find(params[:contact_preference_id])
      redirect_to(:controller =>"readings",:action =>"index")


**<%= radio_button_tag 'contact_preference_id', '#{[](}' %> <%=h r.station.province %>**

Bala wrote:

*<%= radio_button_tag 'contact_preference_id', '#{}' %> <%=h
r.station.province %>

Still facing same problem....

Try this…


you have to add the to the link dynamically which can be used by
javascript .
onclick the radio button you have to add '?
contact_reference_id=checked element id'

Thank you soooooooooo much Nitin

<%= submit_tag "View Details" %>
This has worked for me.

Thanks and Regards,