Rails 上的单选按钮 [英] Radio buttons on Rails

查看:50
本文介绍了Rails 上的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

制作与 Ruby on Rails 中某个问题相关的单选按钮的正确方法是什么?目前我有:

<label for="theme">主题:</label><br><%= radio_button_tag 'theme', 'plain', true %>清楚的<br><%= radio_button_tag 'theme', 'desert' %>沙漠<br><%= radio_button_tag 'theme', 'green' %>绿色的<br><%= radio_button_tag 'theme', 'corporate' %>公司的<br><%= radio_button_tag 'theme', 'funky' %>时髦

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

解决方案

之前的帖子,略有扭曲:

<label for="theme">主题:</label><% [ 'plain', 'desert', 'green', 'corporate', 'funky' ].每个都做 |主题|%><br><%= radio_button_tag 'theme', 主题, @theme == 主题 %><%=theme.humanize %><%结束%>

哪里

@theme = params[:theme]

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?

解决方案

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]

这篇关于Rails 上的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆