check_box_tag(name, value = "1", checked = false, options = {}) public

Creates a check box form input tag.

Options

  • :disabled - If set to true, the user will not be able to use this input.
  • Any other key creates standard HTML options for the tag.

Examples

  check_box_tag 'accept'
  # => <input id="accept" name="accept" type="checkbox" value="1" />

  check_box_tag 'rock', 'rock music'
  # => <input id="rock" name="rock" type="checkbox" value="rock music" />

  check_box_tag 'receive_email', 'yes', true
  # => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />

  check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
  # => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />

  check_box_tag 'eula', 'accepted', false, :disabled => true
  # => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
Show source
Register or log in to add new notes.
July 23, 2008
8 thanks

Pass id collections with check box tags

It can be useful to pass a collection of ids to a controller, especially in the case of a has_many relationship, for example:

 User has_many Roles

In your view you could have something like:

  <ul>
    <% @roles.each do |role| %>
        <li>
          <%= check_box_tag 'role_ids[]', role.id -%>
          <%= h role.name -%>
        </li>
    <% end %>
  </ul>

Note the square brackets after role_ids - this is important for passing a collection through to the controller.

If you place this in a form and submit it, you can expect to see a param passed into the controller that looks like:

 "role_ids"=>["1", "2", "3"]
August 25, 2008 - (v1.1.6 - v2.1.0)
2 thanks

Delete collections with check box tags

Following autonomous’ directions works wonders on /edit but needs slight modifications when dealing with pagination on /index.

In a /index type listing page we can no longer assume that the list of ids coming back represents changes to all objects so we need to provide some context, that the list of object modifications in our params array is a list of modifications for some set of objects.

We can only assume subsets because pagination or filtering may reduce the set of objects we’re working on.

In our case we had a user management page which listed all users and showed whether they were activated or not. The following code is what we used to ensure that modifications to the first page of objects wouldn’t affect all the other pages.

index.rhtml

  <% @users.each do |user| %>
    <%= hidden_field_tag('seen[]', user.id) -%>
    <%= check_box_tag 'activated[]', user.id -%>
  <% end %>

role_controller.rb

  def index
    if request.post?
      activated_ids = params[:activated].collect {|id| id.to_i} if params[:activated]
      seen_ids = params[:seen].collect {|id| id.to_i} if params[:seen]

      if activated_ids
        seen_ids.each do |id|
          r = User.find_by_id(id)
          r.activated = activated_ids.include?(id)
          r.save
        end
      end
    end
  end