method

check_box_tag

rails latest stable - Class: ActionView::Helpers::FormTagHelper
check_box_tag(name, *args)
public

Creates a check box form input tag.

Options

  • :value - The value of the input. Defaults to "1".

  • :checked - If set to true, the checkbox will be checked by default.

  • :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" />

6Notes

Pass id collections with check box tags

autonomous · Jul 23, 200814 thanks

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:

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

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"]

Set ids when using a collection of values

greeneggs614 · Jun 14, 20111 thank

The trick to getting the helper to populate a unique HTML ID and for rails to recognise a collection is to give the helper a unique 'name' and to set the :name symbol to parameter with an array symbol '[]'.

<% Cars.each do |c| %>
<%= check_box_tag "car_ids[#{c.id}]", c.id, :name => "car_ids[]" %>
<% end %>

Using an unobtrusive Ajax (UJS) :onchange call to the controller#action

gwshaw · Jan 23, 20131 thank

An +:onchange+ call can be made using the Rails 3 +jquery-ujs+ helper in the form: check_box_tag( name, value, checked, html_and_other_options) For example: select_tag( "my_tag_id", entity.id, class: "progress bar update_msg", disabled: disabled? data: { remote: true, url: url_for( action: :my_controller_action, id: my_id) // application symbols progress_bar: "progress_bar_div_id", update: "message_div_id" } ) The +jquery_ujs+ looks for +data-remote+ and +data-url+. These can be spelled-out or placed in the +data+ hash. The url must be formed as select_tag does not call +url_for+, unlike some of the other related tags. Values for application symbols can also be passed through. jQuery triggers will work on the Ajax events that occur. This generates the following:

In this example, by tying into the events the program makes visible an existing hidden progress bar while awaiting a server response, and then displays a +div+ containing a message returned by the server and hides the progress bar. If the +div+ contains a +class=+ for +notice+ or +error+, then they will fade out. $(".layout") .on('ajax:beforeSend', ".progress_bar", function(){ // get id of element to make visible var progress_bar_id = '#' + this.getAttribute('data-progress-bar'); $(progress_bar_id).show(); }) .on('ajax:complete', ".progress_bar", function(){ // get id of element to hide var progress_bar_id = '#' + this.getAttribute('data-progress-bar'); $(progress_bar_id).hide(); }) .on('ajax:complete', ".update_msg", function(evt, xhr, options){ // get id of element to contain message var update = this.getAttribute('data-update'); $("#" + update).replaceWith($(xhr.responseText).attr("id", update)); // cause responses with these classes to fade away... $('.notice').fadeOut(2500); $('.error').fadeOut(8000); });

Use dom_id( resource_instance ) to create the HTML id

webdevotion · Jun 17, 20131 thank

In the notes on this page people use: car_ids_#{c.id}

But you can use this function in stead:
dom_id(c)

Set ids when using a collection of values (cont.)

schmidt · Jan 10, 2012

Concerning greeneggs614's post:

The following version would be a bit more intention revealing while providing the same output:

<% Car.each do |c| %>
<%= check_box_tag "car_ids[]", c.id, :id => "car_ids_#{c.id}" %>
<% end %>

Set ids when using a collection of values (cont.)

rafaelkin · Jul 20, 2012

Regarding schmidt's post.

The following will not have the expected behavior:

<% Car.each do |c| %>
  <%= check_box_tag "car_ids[]", c.id, :id => "car_ids_#{c.id}" %>
<% end %>

But, if you put the "checked" option to false (or true), it will.

<% Car.each do |c| %>
  <%= check_box_tag "car_ids[]", c.id, false, :id => "car_ids_#{c.id}" %>
<% end %>