views:

266

answers:

1

Hello, There has to be a more efficient way in Rails of doing this:

<p>
    <%= f.label :level %>
    <%= f.radio_button :level, "0" %> <%= f.label :level, 0 %>
    <%= f.radio_button :level, "1" %> <%= f.label :level, 1 %>
    <%= f.radio_button :level, "2" %> <%= f.label :level, 2 %>
    <%= f.radio_button :level, "3" %> <%= f.label :level, 3 %>
    <%= f.radio_button :level, "4" %> <%= f.label :level, 4 %>
    <%= f.radio_button :level, "5" %> <%= f.label :level, 5 %>
</p>

The db has a column "level" that can have values from 0-5. I need the current value to be displayed in HTML (ie checked="checked") as this code does. There has to be a more succinct way in Rails of doing this. Any takers?

+3  A: 

It's easy enough with a loop:

<p>
    <%= f.label :level %>
    <% 0.upto(5) do |level| %>
      <%= f.radio_button :level, level.to_s %> <%= f.label :level, level %>
    <% end %>
</p>
Pesto