Radio buttons on Rails

后端 未结 5 906
迷失自我
迷失自我 2020-12-12 19:08

Similar to this question: Checkboxes on Rails

What\'s the correct way of making radio buttons that are related to a certain question in Ruby on Rails? At the moment

5条回答
  •  渐次进展
    2020-12-12 19:41

    I would suggest having a look at formtastic

    It makes radio button and check box collections vastly easier and more concise. Your code would look like so:

        <% semantic_form_for @widget, :html => {:class => 'my_style'} do |f| %>
    <%= f.input :theme, :as => :radio, :label => "Theme:", 
    :collection =>  [ 'plain', 'desert', 'green', 'corporate', 'funky' ] %>
    <% end %>
    

    Formtastic is largely unobtrusive and can be mixed and matched with the "classic" form builders. You can also override the formtastic css class for the form as I did above with
    :html => {:class => 'my_style'}

    Have a look at the pertinent Railscasts.

    Update: I've recently moved to Simple Form which has similar syntax to formtastic but is more lightweight and especially leaves the styling to your own css.

提交回复
热议问题