2011-08-11 62 views

回答

15

收音機

<% ["Peter", "Jhon", "Carl"].each do |item| %> 
    <%= f.radio_button :name, item %> <%= item %><br /> 
<% end %> 

複選框:

<% ["Peter", "Jhon", "Carl"].each do |item| %> 
    <%= f.check_box :name, item %> <%= item %><br /> 
<% end %> 
1

如果你有你的項目是在哈希,例如在一個恆定的,你在課堂上存儲:

# fruits.rb 

# Fruit has a field 'kind' which is stored as an integer but displayed as a string: 
KINDS={0=>'bananas',1=>'plantains',2=>'baby bananas'} 


#fruits/new.html.erb 

<% Fruit::KINDS.each do |val,name| %> 
    <%= f.radio_button :kind, val %> <%= name %><br /> 
<% end %>