Paginate trouble

Well, I added will_paginate to my application. Though when I attempt to
use I get an action controller error.

here is the app/controller/community_controller file I created:

class CommunityController < ApplicationController
  helper :profile

  def index
    @title = "Community"
    @letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
    if params[:id]
      @initial = params[:id]
      @pages, specs = paginate(:specs,
                               :conditions => ["last_name LIKE ?",
@initial+"%"],
                               :order => "last_name, first_name")
      @users = specs.collect { |spec| spec.user }
    end
  end

  def browse
  end

  def search
    if params[:q]
      query = params[:q]
      # First find the user hits...
      @users = User.find_by_contents(query, :limit => :all)
      # ...then the subhits.
      specs = Spec.find_by_contents(query, :limit => :all)
      faqs = Faq.find_by_contents(query, :limit => :all)

      # Now combine into one list of distinct users sorted by last name.
      hits = specs + faqs
      @users.concat(hits.collect { |hit| hit.user }).uniq!
      # Sort by last name (requires a spec for each user).
      @users.each { |user| user.spec ||= Spec.new }
      @users = @users.sort_by { |user| user.spec.last_name }
    end
  end
end

The message is:

NoMethodError in CommunityController#index

undefined method `paginate' for #<CommunityController:0xb6bd4ba4>

Did you restart your server?

Brandon

Hello,

I’m still new to Rails, but installed will_paginate yesterday with no problems. I believe pagination is a method from your model, not the controller. Thus

@pages, specs = paginate(:specs,

should become

@pages, specs = Community.paginate(:specs,

or whatever the model is.

Hope this helps.

Regards
k776

Kieran P wrote:

Hello,

I'm still new to Rails, but installed will_paginate yesterday with no
problems. I believe pagination is a method from your model, not the
controller. Thus

@pages, specs = paginate(:specs,

should become

@pages, specs = Community.paginate(:specs,

or whatever the model is.

Hope this helps.

Regards
k776

On Fri, May 23, 2008 at 7:19 AM, Sean Six
<rails-mailing-list@andreas-s.net>

When I change that it throws the error:

NameError in CommunityController#index

undefined local variable or method `spec' for
#<CommunityController:0xb6b0d504>

      @pages, specs = paginate(:specs,
                               :conditions => ["last_name LIKE ?",
@initial+"%"],
                               :order => "last_name, first_name")
      @users = specs.collect { |spec| spec.user }

It is confusing. because your 'specs' only can be accessed in controller
not in view. what is variable do you use for pagination? 'specs' or
'@users'?

Kieran P wrote:

@pages, specs = Community.paginate(:specs,

you can not use it because :specs is different table name than of
community.
@pages, @specs = Spec.paginate :conditions => ["last_name LIKE ?",
@initial+"%"], :order => "last_name, first_name", :page => params[:page]

Reinhart
http://teapoci.blogspot.com

Rails Terrorist wrote:

      @pages, specs = paginate(:specs,
                               :conditions => ["last_name LIKE ?",
@initial+"%"],
                               :order => "last_name, first_name")
      @users = specs.collect { |spec| spec.user }

It is confusing. because your 'specs' only can be accessed in controller
not in view. what is variable do you use for pagination? 'specs' or
'@users'?

Kieran P wrote:

@pages, specs = Community.paginate(:specs,

you can not use it because :specs is different table name than of
community.
@pages, @specs = Spec.paginate :conditions => ["last_name LIKE ?",
@initial+"%"], :order => "last_name, first_name", :page => params[:page]

Reinhart
http://teapoci.blogspot.com

I believe it is tied in with user.

This is the app/views/community/_user_table.rhtml file:

<% if @users and not @users.empty? %>
<table class="users" border="0" cellpadding="5" cellspacing="1">
  <tr class="header">
    <th>Name</th> <th>Age</th> <th>Gender</th> <th>Location</th>
  </tr>
  <% @users.each do |user| %>
  <tr class="<%= cycle('odd', 'even') %>">
    <td><%= link_to user.name, profile_for(user) %>
    </td>
    <td><%= user.spec.age %></td>
    <td><%= user.spec.gender %></td>
    <td><%= user.spec.location %></td>
  </tr>
  <% end %>
  <% if paginated? %>
  <tr>
    <td colspan="4" align="right">
    Pages: <%= pagination_links(@pages, :params => params) %>
    </td>
  </tr>
  <% end %>
</table>
<% end %>

If that helps answer your question.

I have solution with regular pagination (not will_paginate)

@spec = Spec.find(:all, :conditions => ["last_name LIKE
?",@initial+"%"], :order => "last_name, first_name")

@user_collection = @spec.collect { |spec| spec.user }

@pages, @users = paginate_collection @user_collection, :page =>
@params[:page]

private

def paginate_collection(collection, options = {})
    default_options = {:per_page => 10, :page => 1}
    options = default_options.merge options

    pages = Paginator.new self, collection.size, options[:per_page],
options[:page]
    first = pages.current.offset
    last = [first + options[:per_page], collection.size].min
    slice = collection[first...last]
    return [pages, slice]
end

Render From:
http://teapoci.blogspot.com/2008/04/paginate-array-objects-in-ruby.html

Good LUcK,
Reinhart