Hi,

am using this wonderful plugin acts_as_ferret and according to the
tutorial at http://railsenvy.com/2007/2/19/acts-as- … rial#basic

I worked it out except the pagination feature.

If I have 12 records and I give limit to 10, its correctly displaying 10
records in the first page and is giving the link to the second page too.
But when I go to the next page I find the same 10 records instead of the
next 2 records. Also the result count is showing only 10 instead of 12
thats the right count.


Here's my code:

Controller:

def search
      @users = User.available_users
      @user = User.find(session[:user_id])
  @query = params[:query] || ''
@total, @user_profiles = UserProfile.multi_search(@query, [ WorkProfile,
SchoolProfile ], :page => (params[:page]||1))
  @pages = pages_for(@total)
  unless @query.blank?
  @results = UserProfile.find_by_contents @query
  end
end



model:

def self.multi_search(q, additional_models = [], options = {})
   return nil if q.nil? or q==""
   default_options = {:limit => 1, :page => 1}
   options = default_options.merge options
      # get the offset based on what page we're on
   options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
      # now do the query with our options
      results = UserProfile.find_by_contents(q, options)
   return [results.total_hits, results]
end




view:

<div id="header">
<% if @results  -%>
<div id="result">
<table cellpadding="1" cellspacing="1" width="auto">
<tr valign="top" class="resultdetails">

<td>Your search for <font size="3"><b><%= h @query %></b></font>
returned <font size="3"><b><%= @results.size %></b></font> Results:</p>
<ul>
</td>
</tr>
</table>
  <% @results.each { |result| -%>
  <li><%= link_to result.user.name, :controller => 'user_profiles',
:action => 'list_user', :id=> result.user %></li><br />

  <% if result.user.user_pic %>
  <img  src="<%=image_path url_for( :controller => "user_pics", :action
=> "show_thumb",
:id => result.user.user_pic ) %>" />
<% end %>
  <% } -%>

</ul>
<br />

<% end -%>

</div>
</div>

<%= link_to 'Previous page', { :page => @pages.current.previous, :query
=> @query} if @pages.current.previous %>
<%= pagination_links(@pages, :params => { :query=> @query }) %>
<%= link_to 'Next page', { :page => @pages.current.next, :query =>
@query} if @pages.current.next %>


Please do let me know how I get my pagination right...

Cheers

Cass
-- 
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to