Rails 上的单选按钮

发布于 2024-07-15 06:19:00 字数 752 浏览 3 评论 0原文

与此问题类似: Checkboxes on Rails

What's the right way of Making radio Buttons that are related to a certain 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>

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

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 I have:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧街凉风 2024-07-22 06:19:00

正如这篇上一篇文章中那样,有一点小小的不同

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

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]
橘寄 2024-07-22 06:19:00

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

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

<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>
双马尾 2024-07-22 06:19:00

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

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.

= 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
可爱暴击 2024-07-22 06:19:00

我建议看看 formtastic

它使单选按钮和复选框集合变得更加容易和简洁。 您的代码看起来像这样:

    <% 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 基本上不引人注目,可以与“经典”表单构建器混合和匹配。 您还可以像我上面所做的那样覆盖表单的 formattastic css 类
<代码>:html => {:类=> 'my_style'}

查看相关的 Railscast。

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

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.

﹉夏雨初晴づ 2024-07-22 06:19:00

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

radio_button_tag 的 Rails 文档

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

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

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.

rails docs for radio_button_tag

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文