Ruby-on-rails Rails 上的单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/623051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:02:30  来源:igfitidea点击:

Radio buttons on Rails

ruby-on-railsrubywebformsradio-button

提问by alamodey

Similar to this question: Checkboxes on Rails

类似于这个问题:Rails 上的复选框

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

在 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>

I also want to be able to automatically check the previously selected items (if this form was re-loaded). How would I load the params into the default value of these?

我还希望能够自动检查以前选择的项目(如果重新加载此表单)。我如何将参数加载到这些默认值中?

回答by vladr

As in this previous post, with a slight twist:

就像在一篇文章中一样,略有不同:

<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>

Where

在哪里

@theme = params[:theme]

回答by dazonic

Same as V's, but has associated labels with each radio button. Clicking the label checks the radio button.

与 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>

回答by Daniel X Moore

Using Haml, getting rid of needless br tags, and nesting inputs within label so that they may be selected without matching labels to ids. Also using form_for. I would consider this to be following best practices.

使用 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

回答by dirkb

I would suggest having a look at formtastic

我建议看看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'}

Formtastic 在很大程度上不显眼,可以与“经典”表单构建器混合搭配。您还可以像上面那样覆盖表单的 formtastic css 类
:html => {:class => 'my_style'}

Have a look at the pertinent Railscasts.

看看相关的 Railscasts。

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

更新:我最近转向了Simple Form,它的语法与 formtastic 相似,但更轻巧,尤其是将样式留给您自己的 css。

回答by scunliffe

Hmm, from the docs I don't see how you can set the ID on the radio buttons... the label's for attribute tries to link to the ID on the radio.

嗯,从文档中我看不到如何在单选按钮上设置 ID...标签的 for 属性试图链接到收音机上的 ID。

rails docs for radio_button_tag

radio_button_tag 的 rails 文档

That said, from the doc, that first param is the "name"... which if that is what it is creating, shouldgroup them alltogether. If not, maybe its a bug?

也就是说,从文档来看,第一个参数是“名称”……如果这是它正在创建的内容,则应该将它们组合在一起。如果没有,也许它是一个错误?

Hmm, wonder if these have been fixed: http://dev.rubyonrails.org/ticket/2879http://dev.rubyonrails.org/ticket/3353

嗯,想知道这些是否已修复:http: //dev.rubyonrails.org/ticket/2879 http://dev.rubyonrails.org/ticket/3353