Rails 上的单选按钮
与此问题类似: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如这篇上一篇文章中那样,有一点小小的不同
:
As in this previous post, with a slight twist:
Where
与 V 相同,但每个单选按钮都有关联的标签。 单击标签会检查单选按钮。
Same as V's, but has associated labels with each radio button. Clicking the label checks the radio button.
使用 Haml,摆脱不必要的 br 标签,并将输入嵌套在标签内,以便可以在不将标签与 id 匹配的情况下选择它们。 还使用form_for。 我认为这是遵循最佳实践。
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.
我建议看看 formtastic
它使单选按钮和复选框集合变得更加容易和简洁。 您的代码看起来像这样:
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:
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.
嗯,从文档中我看不出如何在单选按钮上设置 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