# Adding error with array of checkbox

**URL:** <https://www.ruby-forum.com/t/adding-error-with-array-of-checkbox/145948>\
**Category:** Rails\
**Created:** [August 15, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/adding-error-with-array-of-checkbox/145948 "2008-08-15T12:20:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![khim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb8c5e/32.png) [@khim](https://www.ruby-forum.com/u/khim)\
**Post date:** [August 15, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/adding-error-with-array-of-checkbox/145948/1 "2008-08-15T12:20:58Z")

</div>

def group\_permission  
@role\_combo = Role.find(:all, :select =\> ‘id,  
role\_name’).collect { |r| [r.role\_name, r.id] }  
@roles = Role.find(:all, :select =\> ‘id, role\_name’)  
@pages = Page.find(:all)  
#@page\_roles = PagesRole.find\_by\_sql(“Select `add`, `update`,  
`delete` From pages\_roles”)  
@page\_list = Page.find(:all, :select =\> ‘id, page\_name’)  
@permission = PagesRole.find\_by\_sql("Select role\_id,  
GROUP\_CONCAT(CAST(page\_id AS CHAR)) As pages, " +  
"GROUP\_CONCAT(CONCAT(CAST(pr.added AS CHAR), " +  
"CAST(pr.updated AS CHAR), CAST(pr.deleted AS CHAR))) AS  
permissions " +  
"From pages\_roles as pr " +  
"Group By role\_id " +  
“Order By role\_id, page\_id”).collect { |p| p.attributes  
}  
if request.post?  
begin #2  
form = params[:pages\_role]  
rec\_affected = 0  
PagesRole.transaction do  
#clear the old records first  
rec\_affected = PagesRole.delete\_all([“role\_id = ?”,  
form[:role\_id]])  
#add new record  
if params[:chbox]  
debugger  
#params[:chbox].each do |key,value|  
1.upto(params[:chbox].length-18) do |check|

```
          if params[:chbox] == 1
             params[:chbox].squeeze!(" ")
             params[:chbox].split(0)
          end
              pr = PagesRole.new
            pr.role_id = params[:pages_role][:role_id]
              pr.page_id = check.to_i
            pr.added = [check]
            pr.updated = [check]
            pr.deleted = [check]
            pr.save!
      end
    end

    end
  rescue => e
    puts "#{e}====================="
    flash[:notice] = "Permission on <b>" +

```

Role.find(params[:pages\_role][:role\_id]).role\_name +  
" group could not be saved"  
logger.error(e)  
else  
if rec\_affected \> 0  
flash[:notice] = “Permission on **" +  
Role.find(params[:pages\_role][:role\_id]).role\_name +  
"** group was successfully updated”  
else  
flash[:notice] = “Permission on **" +  
Role.find(params[:pages\_role][:role\_id]).role\_name +  
"** group was successfully added”  
end  
redirect\_to :action =\> ‘group\_permission’  
end  
end  
end

Hi sir, i have an error with adding data with array of checkbox when i  
added the element in array is increase for examle, in my checkbox i  
have 27 checkbox but when i checked on someone checkbox it has added  
one more 28 example chbox[] = {“1”,“0”, “0”, “0”, “0”, “0”, “0”, “0”,  
“0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”,  
“0”, “0”, “0”, “0”, “0”, “0”}

if i check two time chbox[] = {“1”,“0”, “0”, “0”, “1”, “0”, “0”, “0”,  
“0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”,  
“0”, “0”, “0”, “0”, “0”, “0”, “0”}

i hope u will help my by regard…  
thank u.

---

<div class="post-metadata">

**Author:** ![khim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb8c5e/32.png) [@khim](https://www.ruby-forum.com/u/khim)\
**Post date:** [August 15, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/adding-error-with-array-of-checkbox/145948/2 "2008-08-15T12:29:10Z")

</div>

> “0”, “0”, “0”, “0”, “0”, “0”, “0”}  
> This is a sideeffect of how checkboxes work. As per the html spec, a  
> checked checkbox submits its value, but an unchecked one submits  
> nothing.  
> However a lot of the time, its far more helpgul to get (for example) 1  
> or 0 than 1 or nothing.  
> if you use the check\_box helper then rails generates a hidden field  
> tag with the same name and value 0. if the checkbox is unchecked this  
> value wins (because rails ignores duplicate parameter names), if its  
> checked then the checkbox wins.  
> This doesn’t work with arrays though, since they are implemented via  
> parameter name repetition.

You’ll be better off using check\_box\_tag rather than check\_box and  
having the box’s value be the id of the role)

Fred
