Persist checked checkboxes in pagination


#1

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%>

<% 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.


#2

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.


#3

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