How to make a check_box checked in rails?
Asked Answered
K

11

40

I made checkboxes using the following 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?

Kwon answered 28/11, 2012 at 23:55 Comment(2)
#7372711 #587104Dorchester
neither of these questions address the OP's use casePaymaster
P
61

Here's how to do it as of rails 4, I didn't check older documentation.

<%= 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.

Paymaster answered 15/2, 2014 at 1:34 Comment(3)
How do you give it a label?Hankering
FYI, using a String like "1" or an integer like 1 does not work. So if you have those, convert them to a Boolean with to_boolean.Deputize
Also works with f.check_box ex: f.check_box :my_check,{ class: ' custom-control-input', checked: @model.check }Ezekiel
S
14

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

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

As I mentioned here

Stallion answered 16/5, 2016 at 0:43 Comment(1)
This is the most useful answer, even though the question does not address it. Odds are the situation will appear frequently.Corydalis
C
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.

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:

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.

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.

<%= 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:

<%= f.check_box :enabled %>
Calhoun answered 29/11, 2012 at 1:9 Comment(0)
T
3

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

<%= check_box_tag "name", value, true %> # true or false
Talkative answered 16/3, 2017 at 12:15 Comment(0)
K
2

The check_box_tag instead of check_box has a way to set that it's been checked.

Kwon answered 29/11, 2012 at 0:11 Comment(0)
P
1

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

Predate answered 29/11, 2012 at 0:50 Comment(0)
S
1

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") %>
Salba answered 1/12, 2015 at 17:14 Comment(2)
You mean def check_if_true(item) item == true || item == 'true' || item == 1 || item == '1' endRedwine
@Redwine You mean def check_if_true(item) ActiveModel::Type::Boolean.new.cast(item) endTrust
D
1

This answer is only related to f.check_box

f.check_box can take up to 4 arguments:

  1. input name
  2. hash containing options such as html class, id, etc
  3. which value represents "checked" (defaults to "1")
  4. which value represents "unchecked" (default to "0")

So if your boolean field is stored as either 1 or 0 (default for Rails) then everything is fine and well. Rails will recognize whether the field is checked or not.

However, if you're using a different data storage or a framework which is storing booleans in another format, you need to specify which values correspond to checked and unchecked.

Here's an example:

<%= f.check_box :accept_privacy_policy, { class: 'my-class' }, "true", "false" %>


References

Dmz answered 1/3, 2021 at 20:23 Comment(0)
T
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,

# 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>
Thibeault answered 3/11, 2015 at 17:47 Comment(0)
T
0

This is how I did this now in 2020.

<%= form.check_box :with_cost, checked: true, class: '', type: 'checkbox', id: 'cost' %>

With the checked: true attribute.

Hope this helps.

Tabasco answered 21/10, 2020 at 11:31 Comment(0)
A
0

If you are using the form helper in Rails 5.1 + to build your checkbox you have to pass in a hash the values with specific order.

= form.check_box :open_after, { class: 'form-control mt-1', data: {}, checked: true }, true, false

As you can see, inside the hash, first is class, next data and last checked. This works for me with Rails 6.0

Apostolate answered 2/11, 2020 at 22:28 Comment(1)
The order of the items in a hash is irrelevant.Dmz

© 2022 - 2024 — McMap. All rights reserved.