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.

View

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

Thanks,
Veena

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’, “#{r.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 -
if request.post?
#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,
Nitin.

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

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)

readings/index.html.erb

<% for r in @readings %>
<%= radio_button_tag ‘contact_preference_id’, “#{r.id}”, false %>
<%=h r.station.province %>
<%end%>
<%= link_to “View Details”, :controller => “weather”, :action =>
“index” %>

Weather Controller
def index
if request.post?
station = Station.find(params[:contact_preference_id])
@readings=CurrentReading.find_all_by_station_id(station)
else
redirect_to(:controller =>“readings”,:action =>“index”)
end
end

Thanks,
Veena

Bala wrote:

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

Still facing same problem…
Thanks,
veena

Try this…
INSIDE VIEW -

<% form_tag :controller => “readings”, :action =>“index” do %>
<% for r in @readings %>

<%= radio_button_tag 'contact_preference_id', "#{r.station.id}", false %><%=h r.station.stats %>
<%end%> <%= submit_tag "View Details" %> <% end %>

INSIDE CONTROLLER -

@readings = CurrentReadings.find(:all)
if request.post?
#do something
station = Station.find(params[:contact_preference_id])
@readings=CurrentReading.find_all_by_station_id(station)
end

INSIDE MODEL-

def stats
“#{self.name}-#{self.unit_id}, #{self.province.nil? ? ‘’ :
self.province+’, '} #{self.state.titleize}”
end

good luck (You really need to modify all this according to your
requirement.
I just took an example case similar to your DATABASE)
Nitin.

you have to add the r.id 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,
Veena

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