ala*_*dey 58 ruby webforms ruby-on-rails radio-button
与此问题类似:Rails上的复选框
在Ruby on Rails中制作与某个问题相关的单选按钮的正确方法是什么?目前我有:
<div class="form_row">
    <label for="theme">Theme:</label>
    <br><%= radio_button_tag 'theme', 'plain', true %> Plain
    <br><%= radio_button_tag 'theme', 'desert' %> Desert
    <br><%= radio_button_tag 'theme', 'green' %> Green
    <br><%= radio_button_tag 'theme', 'corporate' %> Corporate
    <br><%= radio_button_tag 'theme', 'funky' %> Funky
</div>
我还希望能够自动检查以前选择的项目(如果重新加载此表单).如何将参数加载到这些的默认值?
vla*_*adr 75
<div class="form_row">
    <label for="theme">Theme:</label>
    <% [ 'plain', 'desert', 'green', 'corporate', 'funky' ].each do |theme| %>
      <br><%= radio_button_tag 'theme', theme, @theme == theme %>
      <%= theme.humanize %>
    <% end %>
</div>
哪里
@theme = params[:theme]
daz*_*nic 38
与V相同,但每个单选按钮都有相关标签.单击标签会检查单选按钮.
<div class="form_row">
  <p>Theme:</p>
  <% [ 'plain', 'desert', 'green', 'corporate', 'funky' ].each do |theme| %>
    <br><%= radio_button_tag 'theme', theme, @theme == theme %>
    <%= label_tag "theme_#{theme}", theme.humanize %>
  <% end %>
</div>
使用Haml,摆脱不必要的br标签,并在标签内嵌套输入,以便可以选择它们而不将标签与ID匹配.还使用form_for.我认为这是遵循最佳做法.
= form_for current_user do |form|
  .form_row
    %label Theme:
    - [ 'plain', 'desert', 'green', 'corporate', 'funky' ].each do |theme|
      %label
        = form.radio_button(:theme, theme)
        = theme.humanize