will_paginate and remember checkboxes

How can I remeber which checkbox-es was checked or unchecked when I use paginate (will_paginate). I don't want to save checked and unchecked checkboxes (positions) when I change page, but I want to save at the end, when I choose, what I want.

Anka,

you may be asking something which puzzled me:

will_paginate builds the search parameters into the page links so that if the check boxes were included in a search form and submitted, check box parameter should be included in the page links of the redisplayed page. All you need to do is to remember to set the check box value from the params when as the page is displayed again.

It is a bit of magic that I had not seen explicitly described, although it is implicitly there in the rails cast for simple search form

Tonypm

Anka,

Did you find a solution to this problem? I am in need of the same solution where my check_box_tags are in a partial along with the will_paginate link (ajax with lowpro).

I try do this, but... but I don't know, how to pass ids params. I have something like this:

<%for user in @users%>     <div>         <%=h full_user_name(user)%>         <%=check_box_tag "project[user_ids]", user.id, @project.users.include?(user)%>     </div>   <%end%>   <%= will_paginate @users, :params => {'searchtext' => params[:searchtext], 'project[user_ids]' => ??? }, :remote => {:update => 'users_div'}%>

I also changed little bit will_paginate using http://weblog.redlinesoftware.com/2008/1/30/willpaginate-and-remote-links maybe here is a problem?

So, I can't help... Maybe Tonypm can helps little bit more..., Can You Tonypm?

Can someone help, and explain how to remember checked and unchecked checkboxes (ids) in params?

Hi Anka,

I have discovered (and should have known) that will_paginate is done over GET which means you lose those checkboxes which are changed as you change pages because you are unable to POST any params to the controller. Mislav's solution was to save the checked IDs in a cookie, or even a session by talking to the controller with Ajax on every check/uncheck. I changed my design by implementing a drag & drop feature which uses POST and didn't require any cookies or sessions.

If you did want to implement a sessions method you I think you would have session[:project] = @project in your controller method and then in your partial you would have something like:

<div>    <%=h full_user_name(user)%>    <%=check_box_tag "session[:project][user_ids]", user.id, session[:project].users.include?(user)%> </div>

Although my knowledge is limited, I hope this gets you on the right track.

Hi,

Just noticed that there have been some more posts to this thread. Some time ago now, I played with different ways of handling controls on an index view. I have been developing an approach along the way. I don't have all the details at my fingertips at the moment, but in general, I went along the following lines:

Firstly, the simplest approach is to handle your requests with Get. Then to retain values of controls such as check box values, or select boxes etc, you just need to ensure that the value of the control is defined by the params hash.

My first solution was for a radio button and worked like this.

In the controller action

@search_on = params[:search_on] || 'a' # gets the value or sets the default

In the form   Search Name:   <%= radio_button_tag('search_on','n',@search_on=='n') %>   Search All Fields:   <%= radio_button_tag('search_on','a',@search_on=='a') %>

(You don't need to assign search in the controller, it can all be easily done from params in the view) This is ok for the simple requirements, but I found mapping the fields into searches can become really tedious.

The next approach I took (which may not be to everyone's taste) is to build my search criteria into a new object based on the model being searched. In this way, the search fields are available for display in a form for the model itself, which makes handling the fields really easy.

eg.

The VIEW - (In haml)

- form_for :quote, @filter, :html=>{:method=>:get, :id=>'filter'} do | form>   %b Filter By:   Company   = text_field_with_auto_complete :quote, :name, {:value=>@filter.name, :size=>20}   %label Created By   - opt=(((Quote.find :all, :select=>'distinct created_by').map {|q| q.created_by}).unshift(''))   = form.select(:created_by, opt)   Status   - opt=(((Quote.find :all, :select=>'distinct status').map {|q| q.status}).unshift (''))   = form.select(:status, opt)   = submit_tag 'Search'

In the CONTROLLER index action:

   if params[:quote]       @filter=Quote.new(params[:quote])       session[:quote_filter]=@filter     elsif session[:quote_filter]       @filter=session[:quote_filter]     else       @filter=Quote.new(:status=>'', :converted_to_sale=>'false')     end     @quotes = @filter.search params[:page]

@filter is a new record of the quote model that is used to create the form in the view. I use the params[:quote] if there is one since this has come from the view and is the new search criteria. I save the filter to the session If there are no params, then I get the filter from the session so that the last used search is reinstated In the Quote model, I have a method called search which uses the params to build the search.

Here is another form using observers to eliminate the submit button.

- form_for :supply_order, @filter, :url=>'/supply_orders/index', |      :html=>{:name=>'filter_form', :id=>'filter_form'} do |form| |   %b Filter By:   Order From   = form.collection_select :supplier_id, Contact.suppliers, :id, :name, {:include_blank=>true}   %label Ordered By:   =form.select(:ordered_by,SupplyOrder.ordered_by_list)   = ' - '   %b Complete:   = form.check_box :complete,{}, checked_value = "1", unchecked_value = "0" = observe_field 'supply_order_supplier_id', |                   :url=>'/supply_orders/index', |                   :before => "$ ("+"'filter_form'"+").className='dirty';" , |                   :with=>"'supply_order[supplier_id]=' + value" |