# Persist checked checkboxes in pagination

**URL:** <https://www.ruby-forum.com/t/persist-checked-checkboxes-in-pagination/84893>\
**Category:** Rails\
**Created:** [February 8, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/persist-checked-checkboxes-in-pagination/84893 "2007-02-08T13:37:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jahnvi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Jahnvi](https://www.ruby-forum.com/u/Jahnvi)\
**Post date:** [February 8, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/persist-checked-checkboxes-in-pagination/84893/1 "2007-02-08T13:37:16Z")

</div>

hi, i have used following code in controller for pagination

def view  
page = (params[:page] ||= 1).to\_i  
items\_per\_page = 3  
offset = (page - 1) \* items\_per\_page

@prods=Item.find\_by\_sql(“select \* from items order by id”)

@prod\_pages = Paginator.new(self, @prods.length, items\_per\_page, page)

@items = @prods[offset…(offset + items\_per\_page - 1)]

end

in view.rhtml file i have following code for showing items in view.rhtml  
and checkbox code

\<%=start\_form\_tag (:id=\>@item,:name=\>'frmv' ,:action=\>'compare')%\> \<%=@final%\> \<% if @prods != nil%\> 

```
                <tr align="center">
                <td height=10%></td>
                </tr>
                <tr>
                <td colspan=4 align='center' bgcolor='#abcdef'>Real

```

Estate List  
  
\<% for p in @items%\>

  
  

```
                        <td>
                            <%= h(p.description)%>
                        </td>
                        <td>
                            <%= h(p.price)%>
                        </td>
                        <td>
                            <%= check_box("chk1",

```

p.id,{},“yes”,“no”)%\>

  
\<% end%\>  
  
  
  

|   
\<%= h(p.id)%\>  
 |   
\<% if p.image != nil%\>  
 ![](https://www.ruby-forum.com/images<%=%20h(p.image)%>)  
\<% else %\>  
No Image  
\<% end %\>  
 |
|   
\<%=submit\_tag “Compare”%\>  
 |

  
\<% if @prod\_pages.page\_count \> 1 %\>  
\<%= pagination\_links @prod\_pages %\>  
\<% end %\>  
\<% else%\>  
No items in shop.  
\<% end%\>

```
 </center>

```

\<%=end\_form\_tag%\>
* * *

to view selected data my code in controller is ==

def compare  
@data=""  
data = params[:chk1]

data.each do |pid,flag|  
if flag == “1”  
@data \<\< pid + “,”  
end  
end

## if @data!="" @data=@data[0,@data.length-1] @newdata=Item.find\_by\_sql(“select \*from items where id in (” +@data +")") end

problem is when i go to next page first page’s selected checkboxes  
becomes unselected, so what to do? my aim is to select items from  
multiple pages and get then on new page named Compare.rhtml to compare  
them.

this problem is really frustrating, plz reply if anybody knows.

---

<div class="post-metadata">

**Author:** ![Jahnvi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Jahnvi](https://www.ruby-forum.com/u/Jahnvi)\
**Post date:** [February 8, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/persist-checked-checkboxes-in-pagination/84893/2 "2007-02-08T13:48:13Z")

</div>

I didn’t go through your code really, but why don’t you use a session  
which is updated with an observe\_form( or observe\_field) on your check  
buttons.

---

<div class="post-metadata">

**Author:** ![Jahnvi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Jahnvi](https://www.ruby-forum.com/u/Jahnvi)\
**Post date:** [September 4, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/persist-checked-checkboxes-in-pagination/84893/3 "2008-09-04T16:57:38Z")

</div>

Charly wrote:

> I didn’t go through your code really, but why don’t you use a session  
> which is updated with an observe\_form( or observe\_field) on your check  
> buttons.

Hi Charly,

I’ve got the same problem than Jahnvi, I’m losing my selected  
check\_boxes when I move from one page to another in my pagination. I’ve  
taken a look at the observe\_field solution that you proposed… but I’m  
not able to find out how to solve my problem with it.

May you help me a bit with this? I’ve got so stuck with it…

Thanks a lot for your help,

Monica
