开发者

How can I use radio buttons properly in Rails

I am doing my first project using Ruby on Rails and need to display a set of radio buttons. It should开发者_如何学运维 behave exactly like a selection list. For usability reasons, I need it in a radio buttons format.

In my project, I use the collection select which also allows me to display on the edit page as follows:

select('project','project_type_id',@project_types.collect{|project_type|[project_type.name,project_type.id]}) <br>

I need something exactly like this, (especially the ability to display the selected value in the edit page) but using radio buttons.

I did a Google search and read the entire Rails guides on radio buttons but I can't find the answer.

How can I do this?


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}) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜