Ruby-on-rails 如何在rails中检查check_box?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13616554/
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:06:11  来源:igfitidea点击:

How to make a check_box checked in rails?

ruby-on-railsruby-on-rails-3checkboxruby-on-rails-3.2form-helpers

提问by user1779563

I made checkboxes using the following rails form helper:

我使用以下 rails 表单助手制作了复选框:

<%= check_box("tag", tag.id) %>

However, I need to make some of them checked by default. The rails documentation doesn't specify how to do this. Is there a way? How?

但是,我需要默认检查其中一些。rails 文档没有指定如何执行此操作。有办法吗?如何?

回答by John Bachir

This has a very straightforward solution that is directly supported by check_box(at least as of rails 4, I didn't check older documentation)

这有一个非常简单的解决方案,由check_box(至少从 rails 4 开始,我没有检查旧文档)直接支持

<%= check_box("tag", tag.id, {checked: true}) %>

This will make the checkbox checked. Of course instead of trueyou will put in some logic which determines if each one is checked.

这将使复选框被选中。当然,而不是true您将放入一些逻辑来确定是否检查每个。

回答by GEkk

If you need the check_box to be checked on new, and correctly filled on edityou can do:

如果您需要选中 check_boxnew并正确填写,edit您可以执行以下操作:

<%= f.check_box :subscribe, checked: @event.new_record? || f.object.subscribe? %>

<%= f.check_box :subscribe, checked: @event.new_record? || f.object.subscribe? %>

As I mentioned here

正如我在这里提到的

回答by Dan Polites

The rails docs do say how to have it checked and it depends on the object. If you don't have an instance object to use with check_box, then your best option is to use the check_box_tag as mentioned. If you do, read on.

rails 文档确实说明了如何检查它,这取决于对象。如果您没有与 check_box 一起使用的实例对象,那么最好的选择是使用前面提到的 check_box_tag。如果你这样做,请继续阅读。

Here's the linkto the docs on the check_box helper. Basically how this works is that you have to have an instance variable defined. That instance variable must have a method that returns an integer or a boolean. From the docs:

这是check_box helper 文档的链接。基本上这是如何工作的,你必须定义一个实例变量。该实例变量必须有一个返回整数或布尔值的方法。从文档:

This object must be an instance object (@object) and not a local object. It's intended that method returns an integer and if that integer is above zero, then the checkbox is checked.

此对象必须是实例对象 (@object) 而不是本地对象。该方法旨在返回一个整数,如果该整数大于零,则选中该复选框。

For example, let's assume you have a @tag instance in your view which has an enabled method. The following snippet would cause the checkbox to be checked when enabled is true on the @tag object and unchecked when it is false. To have it enabled by default, set the enabled attribute to true in your controller. The last two variables are the values that you want to submit with the form when the check box is checked and unchecked.

例如,假设您的视图中有一个 @tag 实例,它有一个已启用的方法。以下代码段将导致复选框在 @tag 对象上启用为 true 时被选中,当它为 false 时取消选中。要默认启用它,请在控制器中将 enabled 属性设置为 true。最后两个变量是您要在选中和取消选中复选框时随表单提交的值。

<%= check_box "tag", "enabled", {}, "1", "0" %>

A lot of times, you'll see the check_box helper used with a form builder. So if form_for was used for the @tag instance, you would more than likely use this snippet:

很多时候,您会看到与表单构建器一起使用的 check_box 助手。因此,如果 form_for 用于 @tag 实例,您很可能会使用以下代码段:

<%= f.check_box :enabled %>

回答by Govind shaw

No need of writing checked: true for rails >= 4.0 Simply write

无需编写检查:true for rails >= 4.0 只需编写

<%= check_box_tag "name", value, true %> # true or false

回答by user1779563

The check_box_taginstead of check_boxhas a way to set that it's been checked.

check_box_tag不是check_box有一种方法来设置它已被检查。

回答by Marcelo Alves

Using check_box_tagyou can set it to trueso that it's already checked. More info here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag

使用check_box_tag您可以将其设置为true已选中。更多信息:http: //api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag

回答by bbozo

Problem with all of these solutions is that it doesn't play well with the paramshash on resubmits, so at the moment I'm using something like this,

所有这些解决方案的问题在于它params在重新提交时不能很好地处理哈希,所以目前我正在使用这样的东西,

# ApplicationHelper
def resolve_boolean_parameter resource, attribute, options = {}
  default = options.delete(:default)
  return default unless params[:utf8]

  return params[resource][attribute] == "1"
end

and then in the view:

然后在视图中:

<div><%= f.label :accepts_newsletter, "Receive Newsletters" %>
  <%= f.check_box :accepts_newsletter, :checked => resolve_boolean_parameter(:user, :accepts_newsletter, default: true)  %>
</div>

回答by FlyingV

New Function placed in your Helper

新功能放置在您的助手中

def check_if_true(item)
  ActiveModel::Type::Boolean.new.cast(item)
end

In your View

在你看来

<%= check_box("test", "active", {checked: check_if_true(@test.active) , :multiple => true, :style => "margin-left: 16px;"}, "true", "false") %>