I made checkboxes using the following rails form helper:
我使用以下rails form helper创建了复选框:
<%= 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文档未指定如何执行此操作。有办法吗?怎么样?
8 个解决方案
#1
37
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 true
you will put in some logic which determines if each one is checked.
这将选中复选框。当然不是真的,你会提出一些逻辑来确定每一个是否被检查。
#2
4
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 docs确实说如何检查它,它取决于对象。如果您没有要与check_box一起使用的实例对象,那么最好的选择是使用上面提到的check_box_tag。如果你这样做,请继续阅读。
Here's the link to 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助手上文档的链接。基本上这是如何工作的,你必须定义一个实例变量。该实例变量必须具有返回整数或布尔值的方法。来自文档:
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 %>
#3
3
If you need the check_box to be checked on new
, and correctly filled on edit
you can do:
如果您需要检查新的check_box,并在编辑时正确填写,您可以执行以下操作:
<%= 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
正如我在这里提到的
#4
2
The check_box_tag
instead of check_box
has a way to set that it's been checked.
check_box_tag而不是check_box有一种方法来设置它已被检查。
#5
2
No need of writing checked: true for rails >= 4.0 Simply write
无需写入检查:对于rails> = 4.0,只需写入
<%= check_box_tag "name", value, true %> # true or false
#6
0
Using check_box_tag
you can set it to true
so 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
#7
0
Problem with all of these solutions is that it doesn't play well with the params
hash 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>
#8
0
New Function placed in your Helper
def check_if_true(item)
if(item == 'true' or item == true or item == 1 or item == '1')
return true
else
return false
end
end
In your View
<%= check_box("test", "active", {checked: check_if_true(@test.active) , :multiple => true, :style => "margin-left: 16px;"}, "true", "false") %>
#1
37
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 true
you will put in some logic which determines if each one is checked.
这将选中复选框。当然不是真的,你会提出一些逻辑来确定每一个是否被检查。
#2
4
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 docs确实说如何检查它,它取决于对象。如果您没有要与check_box一起使用的实例对象,那么最好的选择是使用上面提到的check_box_tag。如果你这样做,请继续阅读。
Here's the link to 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助手上文档的链接。基本上这是如何工作的,你必须定义一个实例变量。该实例变量必须具有返回整数或布尔值的方法。来自文档:
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 %>
#3
3
If you need the check_box to be checked on new
, and correctly filled on edit
you can do:
如果您需要检查新的check_box,并在编辑时正确填写,您可以执行以下操作:
<%= 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
正如我在这里提到的
#4
2
The check_box_tag
instead of check_box
has a way to set that it's been checked.
check_box_tag而不是check_box有一种方法来设置它已被检查。
#5
2
No need of writing checked: true for rails >= 4.0 Simply write
无需写入检查:对于rails> = 4.0,只需写入
<%= check_box_tag "name", value, true %> # true or false
#6
0
Using check_box_tag
you can set it to true
so 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
#7
0
Problem with all of these solutions is that it doesn't play well with the params
hash 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>
#8
0
New Function placed in your Helper
def check_if_true(item)
if(item == 'true' or item == true or item == 1 or item == '1')
return true
else
return false
end
end
In your View
<%= check_box("test", "active", {checked: check_if_true(@test.active) , :multiple => true, :style => "margin-left: 16px;"}, "true", "false") %>