Will_paginate

Hello G.s
I hope someone can help me
I’ve been trying but without success to do a pagination using the
will_paginate gem.
When I’m listing all records the system paginates but when I’m doing a
filter to show some records, just show the first five. Someone knows
whats happening?

My code

CONTROLLER

def index

@contract = Contract.new(params[:contract])
page = (params[:page] ||= 1).to_i
@contracts = Contract.search_index({:page => page})
@legal_entities = LegalEntity.all(:select => "CD_PESSOA_JURIDICA,

NM_PESSOA", :joins => [:person])

@persons = Person.all

  respond_to do |format|
    format.html # index.html.erb
  end

end

def list

@contract = Contract.new(params[:contract])
page = (params[:page] ||= 1).to_i
@contracts = Contract.find_by_params(params[:contract])
@legal_entities = LegalEntity.all(:select => "CD_PESSOA_JURIDICA,

NM_PESSOA", :joins => [:person])

@person = Person.all
render :index

end

VIEW
<% form_for(@contract, :url => { :action => “list”}, :onKeyPress =>
“submit();” ) do |f| %>

Empresa:
<%= select(:contract, :CD_PESSOA_JURIDICA, @legal_entities.collect{ |p| [ p.NM_PESSOA, p.CD_PESSOA_JURIDICA]}, { :include_blank => true }) %>
Número do contrato:
<%= f.text_field(:CD_CONTRATO, :style=>"width: 90%;", :maxlength=>"7", :onKeypress => "apenasnum(this);")%>
Ano:
<%= f.text_field(:NO_ANO, :style=>"width: 90%;", :onKeypress => "apenasnum(this);")%>
Objeto do contrato:
<%= f.text_field(:DS_OBJETO_CONTRATO, :style=>"width: 80%;", :onKeypress => "apenastex(this);")%>
<%= image_submit_tag("lupa.png", :title => "Pesquisar")%>

<%= link_to ( image_tag("incluir.jpg",:style=>"width: 16px; heigth:16px; border:0", :title =>"Incluir Novo"))+' Incluir Novo', new_contract_path %>

<% if @contracts.empty? %>

Nemhum contrato foi encontrado

<% else %>
<tbody class="zebra">
   <% @contracts.each do |contract| %>
      <tr>
        <td style="padding-left:5px;"><%= link_to

contract.CD_CONTRATO, contract %>






<% end %>



  <tr>
    <td colspan="6" class="paginacao">
      <%= will_paginate @contracts %>

    </td>
  </tr>
Numero Contrato Ano Empresa Objeto Termo Aditivo
<%= contract.NO_ANO %> <%=
contract.legal_entity.person.NM_PESSOA %>
<%=
contract.DS_OBJETO_CONTRATO %>
<%=
“#{contract.additiv_contracts.size} aditivos” %>

Total de <%= @contracts.total_entries %> ítens.
<% end %> <% end %>

Cheers

Raony Vieira ferreira wrote:

CONTROLLER

def index
@contract = Contract.new(params[:contract])
page = (params[:page] ||= 1).to_i
@contracts = Contract.search_index({:page => page})
@legal_entities = LegalEntity.all(:select => “CD_PESSOA_JURIDICA,
NM_PESSOA”, :joins => [:person])
@persons = Person.all
respond_to do |format|
format.html # index.html.erb
end
end

def list
@contract = Contract.new(params[:contract])
page = (params[:page] ||= 1).to_i
@contracts = Contract.find_by_params(params[:contract])
@legal_entities = LegalEntity.all(:select => “CD_PESSOA_JURIDICA,
NM_PESSOA”, :joins => [:person])
@person = Person.all
render :index
end

Seems like this should all be controller work (well, not quite, and this
is based on the mislav-will_paginate gem)…

You could use a generic
@contracts = Contract.paginate :conditions => cond, :page =>
params[:page]

where cond is your scoping criteria, such as:

cond = [‘name LIKE ?’, some_params_entry] or
cond = [‘1=1’] for the everything case

The only difference in your two methods is the Contract.find…

Not knowing what the search_index or find_by_params methods do
precisely, could you create a single build_conditions method in the
Contract model that returns ‘cond’ and covers all your search cases to
simplify your logic?

index and list are awfully similar… do you need both?

Raony Vieira ferreira wrote:

I don’t need both, I tried but I had no success using generic
I think I’ve done something wrong in model.

Skinny controllers, fat models.

Given scoping criteria that may be coming in params, the model should
know how to respond to those params for limiting return sets, and can
adapt its ‘cond’ to account for those. Controller just knows to use the
received cond in its paginate call.

Model:

class Contract
def build_conditions(params)
# the default returns
cond = [‘1=1’]
page = (params[:page] ||= 1).to_i
per_page = 15
if params[:partial_contract_name]
# return a scoping condition here, this can be as complex
# as you want/require
cond = [‘name LIKE ?%’, params[:partial_contract_name]
end
return cond, page, per_page
end
end

Controller:

def index
@contract = Contract.new(params[:contract])
cond, page, per_page = @contract.build_conditions(params)
@contracts = Contract.paginate :conditions => cond, :page => page,
:per_page => per_page
@legal_entities = LegalEntity.all(:select => “CD_PESSOA_JURIDICA,
NM_PESSOA”, :joins => [:person])
@persons = Person.all
respond_to do |format|
format.html # index.html.erb
end
end

should be a nice generic implementation using will_paginate. Again, I
don’t know what was happening in your search_index or find_by_params
methods.

The only difference in your two methods is the Contract.find…

Not knowing what the search_index or find_by_params methods do
precisely, could you create a single build_conditions method in the
Contract model that returns ‘cond’ and covers all your search cases to
simplify your logic?

index and list are awfully similar… do you need both?

I don’t need both, I tried but I had no success using generic
I think I’ve done something wrong in model.
Using generic i have to modify my model?
I’m quite confused with that, because I usually use will_paginate
without filter.

Cheers

I need help.

I input gem ‘will_paginate’ or config.gem ‘will_paginate’

but got the undefined method `paginate’ for #Class:0x653996c

On 8月26æ—¥, 下午11時01分, Raony Vieira ferreira [email protected]

Thanks.

But I config.gem ‘will_paginate’ before the end of environment.rb and
f the will_paginate is already installed but not worked. Got the
undefined method `paginate’ for #Class:0x653996c Why? May you give
me a hint, or sample, let me fix it up.

On 8月28æ—¥, 上午12時05分, Raony Vieira ferreira [email protected]

On 29 August 2010 03:44, Jeff C. [email protected] wrote:

Thanks.

But I config.gem ‘will_paginate’ before the end of environment.rb and
f the will_paginate is already installed but not worked. Got the
undefined method `paginate’ for #Class:0x653996c Why? May you give
me a hint, or sample, let me fix it up.

config.gem must go inside the initializer.run block, not just before the
end.

Post your environment.rb if you think you have it correctly, also the
output of
gem list --local
and the full error that you are getting.

Colin

Jeff C. wrote:

I need help.

I input gem ‘will_paginate’ or config.gem ‘will_paginate’

but got the undefined method `paginate’ for #Class:0x653996c

On 8月26æ—¥, 下午11時01分, Raony Vieira ferreira [email protected]

The correct way is config.gem ‘will_paginate’
but if the will_paginate is already installed should have worked.

Dear Colin,

Thanks.

  1. The environment.rb:

RAILS_GEM_VERSION = ‘2.3.8’ unless defined? RAILS_GEM_VERSION

Bootstrap the Rails environment, frameworks, and default

configuration
require File.join(File.dirname(FILE), ‘boot’)

Rails::Initializer.run do |config|

Settings in config/environments/* take precedence over those

specified here.

Application configuration should go into files in config/

initializers

– all .rb files in that directory are automatically loaded.

config.gem 'will_paginate'

Add additional load paths for your own custom dirs

config.load_paths += %W( #{RAILS_ROOT}/extras )

Specify gems that this application depends on and have them

installed with rake gems:install

config.gem “bj”

config.gem “hpricot”, :version => ‘0.6’, :source => "http://

code.whytheluckystiff.net"

config.gem “sqlite3-ruby”, :lib => “sqlite3”

config.gem “aws-s3”, :lib => “aws/s3”

Only load the plugins named here, in the order given (default is

alphabetical).

:all can be used as a placeholder for all plugins not explicitly

named

config.plugins =

[ :exception_notification, :ssl_requirement, :all ]

Skip frameworks you’re not going to use. To use Rails without a

database,

you must remove the Active Record framework.

config.frameworks -=

[ :active_record, :active_resource, :action_mailer ]

Activate observers that should always be running

config.active_record.observers

= :cacher, :garbage_collector, :forum_observer

Set Time.zone default to the specified zone and make Active Record

auto-convert to this zone.

Run “rake -D time” for a list of tasks for finding time zone

names.
config.time_zone = ‘UTC’

The default locale is :en and all translations from config/locales/

*.rb,yml are auto loaded.

config.i18n.load_path += Dir[Rails.root.join(‘my’, ‘locales’, '*.

{rb,yml}’)]

config.i18n.default_locale = :de

end

  1. The output of gem list --local:
    will_paginate <2.3.14>

  2. The full error MSG:
    NoMethodError in ManageController#index

undefined method `paginate’ for #Class:0x679f940

The manage_c0ntroller.rb:

class ManageController < ApplicationController

def index
list
render :action => ‘list’
end

GETs should be safe (see

http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }

def list

@items = Item.paginate(:page => params[:page], :per_page => 10)

end
#@posts = Post.paginate :page => params[:page], :per_page => 50
def show
@item = Item.find(params[:id])
end

def new
@item = Item.new
end

def create
@item = Item.new(params[:item])
if @item.save
flash[:notice] = ‘Item was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

def edit
@item = Item.find(params[:id])
end

def update
@item = Item.find(params[:id])
if @item.update_attributes(params[:item])
flash[:notice] = ‘Item was successfully updated.’
redirect_to :action => ‘show’, :id => @item
else
render :action => ‘edit’
end
end

def destroy
Item.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end

On 29 August 2010 23:44, Jeff C. [email protected] wrote:

The manage_c0ntroller.rb:

Did you note the first request in my previous email - not to top post?
That means not to post your new message before the quotes of the
previous message, but to insert your reply into the previous message.
Now I have scroll up and down this email looking at what I asked and
back up to see your response. It also makes it less likely that you
will forget to answer a question.

In particular you have not confirmed that you do not get any errors
when you start the server.

Also please can you confirm that if you remove the calls to paginate
and just fetch all records that all works as expected.

Further comments below.

class ManageController < ApplicationController

def index
list
render :action => ‘list’
end

Again I am having to cut and paste from below, if you had not top
posted I would not have had to do this. You previously said the error
is

NoMethodError in ManageController#index
undefined method `paginate’ for #Class:0x679f940

I don’t see any call to paginate so that is a bit odd. Please confirm
exactly what is happening and make sure the code you post matches the
error message. Post the full error trace for the message please.

And once again please don’t top post.

Colin

On 8月30æ—¥, 下午4時08分, Colin L. [email protected] wrote:

In particular you have not confirmed that you do not get any errors
when you start the server.

Yes, confirm there is no any errors when start the server

Also please can you confirm that if you remove the calls to paginate
and just fetch all records that all works as expected.

Yes, all works when remove call to paginate

 end
error message. Â Post the full error trace for the message please.
Here is the development.log:
Processing ManageController#index (for 127.0.0.1 at 2010-08-30
17:30:14) [GET]
[4;35;1mSQL (64.0ms) [0m [0mSHOW TABLES [0m
[4;36;1mItem Columns (403.0ms) [0m [0;1mSHOW FIELDS FROM
items [0m

NoMethodError (undefined method paginate' for #<Class:0x675f980>): app/controllers/manage_controller.rb:25:inlist’
app/controllers/manage_controller.rb:4:in index' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start’

Rendered rescues/_trace (144.0ms)
Rendered rescues/_request_and_response (8.0ms)
Rendering rescues/layout (internal_server_error)

And once again please don’t top post.

Hope the post is correct this time. Thanks.

On 29 August 2010 13:10, Jeff C. [email protected] wrote:

Please don’t top post it makes it more difficult to follow the thread
Thanks

Settings in config/environments/* take precedence over those

config.gem “bj”

[ :exception_notification, :ssl_requirement, :all ]

config.i18n.default_locale = :de

end

That looks OK. When you run script/server I presume you do not get an
error about missing gems? Or any other error?

  1. The output of gem list --local:
    will_paginate <2.3.14>

OK

  1. The full error MSG:
    NoMethodError in ManageController#index

undefined method `paginate’ for #Class:0x679f940

What is the path and filename of that controller? Can you post the
code for the index method?

Colin

NoMethodError (undefined method paginate' for #<Class:0x675f980>): app/controllers/manage_controller.rb:25:inlist’
app/controllers/manage_controller.rb:4:in index' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start’

Rendered rescues/_trace (144.0ms)
Rendered rescues/_request_and_response (8.0ms)
Rendering rescues/layout (internal_server_error)

I don’t know but try, check the will_paginate version and write in
evironment.rb between the “Rails::Initializer.run do |config|” and the
last end this

config.gem ‘will_paginate’, :version => ‘your version’

and restart the server and check

On 8月30æ—¥, 下午7時30分, Raony Vieira ferreira [email protected] wrote:

 c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start'  c:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
 c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start’

Rendered rescues/_trace (144.0ms)
Rendered rescues/_request_and_response (8.0ms)
Rendering rescues/layout (internal_server_error)

I don’t know but try, check the will_paginate version and write in
evironment.rb between the “Rails::Initializer.run do |config|” and the
last end this

config.gem ‘will_paginate’, :version => ‘your version’

and restart the server and check

  1. Checking the version:
    gem list – local -> will_paginate <2.3.14>

  2. and I add below code into environment:

    config.gem ‘will_paginate’, :version => ‘2.3.14’

  3. Restart the server, and got the result:
    undefined method `paginate’ for #Class:0x66a2718

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs