Forum: Ruby on Rails Persist checked checkboxes in pagination

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jahnvi (Guest)
on 2007-02-08 14:37

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

<body>
<%=start_form_tag (:id=>@item,:name=>'frmv' ,:action=>'compare')%>
   <%=@final%>
    <% if @prods != nil%>
      <center>
      <table border="1" cellspacing="5" align="center" height=40%
width=50%>

                    <tr align="center">
                    <td height=10%></td>
                    </tr>
                    <tr>
                    <td colspan=4 align='center' bgcolor='#abcdef'>Real
Estate List</td>
                    </tr>
                    <% for p in @items%>
                        <tr>
                        <td>
                           <%= h(p.id)%>
                        </td>
                            <td>
                                <% if p.image != nil%>
                                    <img src="/images<%= h(p.image)%>"
height=100% width=100%></img>
                                <% else %>
                                    No Image
                                <% end %>
                            </td>

                            <td>
                                <%= h(p.description)%>
                            </td>
                            <td>
                                <%= h(p.price)%>
                            </td>
                            <td>
                                <%= check_box("chk1",
p.id,{},"yes","no")%>
                            </td>
                        </tr>
                     <% end%>
                     <tr>
                        <td>
                            <%=submit_tag "Compare"%>
                        </td>
                     </tr>
                     </table>
                      <% if @prod_pages.page_count > 1 %>
                        <%= pagination_links @prod_pages %>
                    <% end %>
    <% else%>
              No items in shop.
    <% end%>

     </center>

</table>
<%=end_form_tag%>
</body>

--------------------------
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.
Charly (Guest)
on 2007-02-08 14:48
(Received via mailing list)
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.
Monica S. (Guest)
on 2008-09-04 18:57
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
This topic is locked and can not be replied to.