Hello
I am doing my first project in ruby on rails and I need to display a set of radio buttons, which values are taken from a database table. It should behave exactly like a selection list. But due for usability reasons I need it in a radio buttons format.
In my project I use the select collection which also allows me to display in the edit page the user selected value.
For example:
select('project','project_type_id',@project_types.collect{|project_type|[project_type.name,project_type.id]})
I need something exactly like this, (especially the ability to display the selected value in the edit page) but for the radio buttons. I did a googol search and read the entire ruby on rails guides on radio buttons but I can't find the answer.
Is someone can tell me how it can be done in rails
views:
225answers:
1
+1
A:
I suppose you can do it like this in your view
<% @project_types.each do |project_type| %>
<%= radio_button("project", "project_type", project_type.name) %> #assuming you have a name attribute on project_type
<% end %>
If you want a particular radio button to be checked then you can pass the checked option like so
<%= radio_button("project", "project_type", project_type.name, {:checked => true}) %>
nas
2010-02-14 10:24:34
ThanksThe loop is working well but if I put inside it the {: checked => true} it checks all the radio buttons and not just the selected value. I need a way in witch I can select just the user selected value, the same like the collect options does in the select list
winter sun
2010-02-14 11:23:05
Ok I try it again and now only with the loop and without the {: checked => true} option and it is working like magic.When I am in the edit page rails seems to recognize the selected value and checks it according to the user selection
winter sun
2010-02-14 11:29:45
Yes, rails sticks the checked option depending upon the object. I mentioned that option in case you want to check some radio button explicitly.
nas
2010-02-14 12:04:47