Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Select all check_box_tag
7 Posts
Select all check_box_tag


I have a habtm relationship (roles have many rights) that I update with checkboxes. The rights are listed underneath the role on the form in question. The state of the checkbox tells if the role has the right. This is the code:


Has Rights:


My problem is that I need to add two buttons that will check and uncheck all the boxes. I'm thinking Javascript has an answer, but have never worked with it.

Thank you.

Here's an approximation in javascript if you have an array of check boxes:

function check_all(check_box)
for (i = 0; i < check_box.length; i++)
    check_box[i].checked = true ;

So get those check boxes in an array:

Also, somebody correct me if I'm wrong but isn't "@role.rights.include?(right)" unnecessary? Couldn't you iterate through the @role.rights and just set each check box to the value and this will indicate true or false automatically?

Raul’s approach should work, too, but it’s very old-school JavaScript that doesn’t make use of Prototype!

I'm afraid I must agree with Clemens' statement! :)

Okay. I must be missing something. I can't make either one work. I think the problem is that I am completely clueless with Javascript.

This is the view: [code]

Editing role

"form", :object => [:admin, @role], :locals => {:f => f} ) %>



This is the error that I get: NoMethodError in Admin/roles#edit

Showing admin/roles/edit.html.erb where line #10 raised:

undefined method `link_to_fuction' for #

The solution looks very elegant. Please tell me what I am missing that will let me utilize it?

Thank you.

it's your goof :) have a look at the code more careful. there is no link_to_fuction but link_to_function methodprovided by ActionView

"jQuery in Action" by wycats is fine too. Anyway switch to jRails (jQuery)...

Thanx for the solution, it works perfectly!

7 Posts
Login to add your message