botvector.net domain for sale $2k usd (negotiable). Em@il me t0: alex DOT creopolis at gmail DOT com

My Git activity

28 August 2008

will_paginate on AJAX

Do you want will_paginate with AJAX functionality ?
Follow:

  1. Remove will_paginate plugin if you have it (/vendor/plugins/) /or backup it.
  2. in command line: gem sources -a http://gems.github.com
  3. in command line: gem install mislav-will_paginate
Now in config/environment.rb put after Initializer
Rails::Initializer.run do |config|
...
end

require 'will_paginate'
Next step, create new helper: remote_link_renderer.rb .. and copy&paste inside:


class RemoteLinkRenderer < WillPaginate::LinkRenderer
def prepare(collection, options, template)
@remote = options.delete(:remote) || {}
super
end
protected
def page_link(page, text, attributes = {})
@template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote))
#replace :method => :post if you need POST action
end
end

Now, to ajaxize put:
<%= will_paginate :collection, :renderer => "RemoteLinkRenderer",:remote => {:with => "something_you_want", :update => "dom_id_element"} %>

And you are happy-ajax-paginator !
Sample:
<%= will_paginate @events, :renderer => "RemoteLinkRenderer",:remote => {, :update => "events_container", :loading => visual_effect(:appear, "loader"),:complete =>visual_effect(:fade,"loader" ) } %>
will call /events?page=x with AJAX request..
in my EventController:

respond_to do |format|
format.html { }
format.js { render :partial => "events" }
format.xml { render :xml => @events.to_xml }
end

Thats all !!
More info about will_paginate plugin here
Original post here

2 comments:

Unknown said...

Ajax Pagination in less than 5 minutes

http://www.railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes

Anonymous said...

Can this be modified to use multiple controllers?