How can I make a check box default to being "checked" in Rails 1.2.3?
Asked Answered
C

7

43

How can I make a check box default to being "checked" when it is initially displayed?

I've not found a "Rails" way to do this (that works) so I did it with JavaScript. Is there a proper way to do this in Rails? I'm using Rails 1.2.3.

Comminute answered 25/2, 2009 at 15:55 Comment(0)
A
46

If you're using check_box in the context of a form, then the check box will show whatever value that field has.

@user = Person.find(:first)
@user.active = true
check_box 'user', 'active'  #=> will be checked by default

If you're using check_box_tag, the third parameter is the initial state (check_box_tag doc):

check_box_tag "active", 1, true
Audet answered 25/2, 2009 at 16:17 Comment(4)
I'm not reading/writing check_box value to the db, so switching to check_box_tag and using the true switch solved the problem. Thanks!Comminute
The _tag methods are for non-model forms. The non-_tag ones are.Audet
@Audet I'm using a check_box_tag like this <%= check_box_tag "benificiary_id[#{b.id}]",b.id,:name => "benificiary_id[]"%> here how do i check whether any record checked or not during update? by default my update action taking all check box values even if some of them unchecked :(Punchy
@shreekumar s: Two year old question, you should ask a new question.Audet
C
51

Rails 3.x

= form_for(@user) do |f|
  = f.check_box :subscribe, {checked: true, ...}

This sets the checked state to true and should work fine. Note the ruby 1.9.x syntax of the hash, for ruby 1.8.x use hash tag format {:checked => true, ...}

Catercorner answered 11/12, 2011 at 19:54 Comment(5)
I found this to be useful when pulling from a DB recordPractical
this works with Rails 2.3.11 as well. Just use Ruby 1.8.7 hash syntax.Follicle
Works in 4.2 as well.Hanako
I don't know it works in 3.x too, but in 4.2 you don't need {}. So you can just code f.check_box :subscribe, checked: trueMelly
Works for rails 6.0.1Spinous
A
46

If you're using check_box in the context of a form, then the check box will show whatever value that field has.

@user = Person.find(:first)
@user.active = true
check_box 'user', 'active'  #=> will be checked by default

If you're using check_box_tag, the third parameter is the initial state (check_box_tag doc):

check_box_tag "active", 1, true
Audet answered 25/2, 2009 at 16:17 Comment(4)
I'm not reading/writing check_box value to the db, so switching to check_box_tag and using the true switch solved the problem. Thanks!Comminute
The _tag methods are for non-model forms. The non-_tag ones are.Audet
@Audet I'm using a check_box_tag like this <%= check_box_tag "benificiary_id[#{b.id}]",b.id,:name => "benificiary_id[]"%> here how do i check whether any record checked or not during update? by default my update action taking all check box values even if some of them unchecked :(Punchy
@shreekumar s: Two year old question, you should ask a new question.Audet
L
7

Simple

<%= f.check_box :subscribe, checked: "checked" %>
Louis answered 4/11, 2016 at 23:50 Comment(0)
M
6

In your controller's new action, try:

@user = User.new(:male => true)

Where :male is the attribute you want checked by default on the /users/new pages. This will pass the :male attribute to the view with a value of true, resulting in a checked box.

Mohler answered 1/12, 2010 at 0:7 Comment(0)
R
6

I was shocked that none of the answers solve 100% of the problem.
As all suggested solutions will give checked result on edit form, even if the user unchecked the checkbox.

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

Works for rails 4 +, not tested below.

Ralph answered 16/5, 2016 at 0:38 Comment(1)
This should be marked as the correct answer. Thanks!Shrift
J
0

This works on Rails 2.3.x and Rails 3.0.x!

On action new in the controller, the check box is set to true.

# in the controller
def new
  @user = Person.find(:first)
  @user.active = true
end

In the form: the check box is checked on creation (by call new) but if the validation fails the check box remains set with the value that the user has post.

# in the view
<%= form_for ..... |f| %>
  ...
  <%= f.check_box :active %>
  ...
<% end %>

An other way, but not so good (if you want to change the logic you have to make a new migration) is to set :default => 1 in the migration of the given model and attribute.

class CreatePeople < ActiveRecord::Migration
  def self.up
    create_table :people do |t|
      ...
      t.boolean    :active,             :null => false,
                                        :default => 1
      t.timestamps
    end
  end

  def self.down
    drop_table :people
  end
end
Jermainejerman answered 17/4, 2011 at 12:48 Comment(0)
N
0

I did it in that way.

Add hidden field with value 0 ABOVE check_box_tag

<%= hidden_field_tag :subscribe, '0' %>
<%= check_box_tag :subscribe, '1', params[:subscribe] != '0' %>

On server check with != '0'

subscribe = params[:subscribe] != '0'
Nylon answered 23/5, 2014 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.