can anyone suggest me how to remove undefined method 'paginate' in rails 2.3.8
here is my index method which shows index page and code of this is
def index
#clients = Client.paginate :page => params[:page], :per_page => 5
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #clients }
end
end
Undefined method 'paginate' probably indicates that you don't have will_paginate installed.
To get rid of your error you have to choices:
1) Get rid of pagination.
Replace
#clients = Client.paginate :page => params[:page], :per_page => 5
With
#clients = Client.all
2) Install will_paginate by placing the following in your environment file:
config.gem 'will_paginate', :version => '~> 2.3.16'
and then running rake gems:install of cause. :)
Related
I'm trying to update the following code on by controller:
#post = Post.paginate_by_user_id(#user.id, :page => params[:page], :per_page => 20, :order => 'created_at DESC')
This was used most likely used with will_paginate plugin for rails 2
paginate_by_user_id or by anything else is no longer used in current will_paginate gem
Does anyone know how, I can pass the #user.id and use Post.paginate and keep the keep same logic as above?
I have everything else need to make this work.
any help would be greatly appreciated.
Thanks,
Try:
#posts = Post.where(:user_id => #user.id).paginate(:page => params[:page], :per_page => 20).order('created_at DESC')
Or if your User model has a has_many relation to posts:
#posts = #user.posts.paginate(:page => params[:page], :per_page => 20).order('created_at DESC')
I'm trying to use the will_paginate gem but something's wrong. I'm stuck with an undefined method `paginate' error. I read many issues and try a lot of things.
Here's what I've got :
This is my LocationsController.rb:
def index
#locations = Location.all
respond_to do |format|
#locations = #locations.paginate(:page => params[:page], :per_page => 10)
format.html #index.html.erb
format.json { render json: #locations }
end
end
And this is my will_paginate's line in my index.html.erb:
<%= will_paginate #locations %>
And this is the error:
undefined method `paginate' for #<Class:0xaa2e48c>
I also add the require part in my Gemfile:
gem "will_paginate", "~> 3.0.4", :require => nil
And this at the end of my environment.rb:
require "will_paginate"`
Make sure to restart the server after installing the 'will_paginate' gem.
This was the stupid issue in my case.
will_paginate doesn't work like that. paginate is a method of the Location class:
def index
#locations = Location.paginate(page: params[:page], per_page: 10)
respond_to do |format|
format.html
format.json { render json: #locations }
end
end
Moreover, to use will_paginate you should just need to add the line below in your Gemfile, no modification in environment.rb is required:
gem "will_paginate", "~> 3.0.4"
You try to paginate array. Try this:
def index
#locations = Location.paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html #index.html.erb
format.json { render json: #locations }
end
end
If you want to paginate array see Paginating an Array in Ruby with will_paginate
if anyone comes here looking for answer why <%= will_paginate %> is not working or giving an error of wrong arguments . please do this once
1- replace your gem file will paginate gem with this gem "will_paginate", "~> 3.1.7"
2- then run 'bundle install' in your console and restart server.
i hope this will help someone.
I have already implemented a location based search using geocoder and am having trouble integrating the meta_search gem. I'm trying to integrate meta_search into my object_controller index to allow users to filter and sort search results by an objects :attributes after they have already searched by location.
My object_controller:
def index
if params[:search].present?
#objects = Object.near(params[:search], 50, :order => :distance).paginate(:page => params[:page], :per_page => 9)
else
#objects = Object.paginate(:page => params[:page], :per_page => 9)
end
end
Any idea how best to integrate the #search into the index required by the meta_search gem?
Here is what the meta_search github recommends for the index:
def index
#search = Article.search(params[:search])
#articles = #search.all # load all matching records
# #articles = #search.relation # Retrieve the relation, to lazy-load in view
# #articles = #search.paginate(:page => params[:page]) # Who doesn't love will_paginate?
end
Thanks so much,
Will
I believe both the geocoder and meta_search query methods return an ActiveRecord::Relation therefore you should be able to chain them:
#objects = Object.near(params[:search], 50, :order => :distance).search(params[:search]).relation.paginate(:page => params[:page], :per_page => 9)
or if you need the search object to be separate:
#search = Object.near(params[:search], 50, :order => :distance).search(params[:search])
#objects = #search.relation.paginate(:page => params[:page], :per_page => 9)
I am trying to upgrade my rails gem from 2.3.2 to 2.3.11. However, I got some problems with will_paginate 2.3.15 and render json back.
module WillPaginateHelpers
WillPaginate::Collection.class_eval do
alias :to_json_without_paginate :to_json
def to_json(options = {})
hash = { :current_page => current_page,
:per_page => per_page,
:total_entries => total_entries,
:total_pages => total_pages,
:items => to_a
}
hash.to_json(options)
end
end
end
Previously, the code above could work with:
#products = Product.paginate(:page => 1, :per_page => 20)
render :json => #products
However, with rails 2.3.11, it comes up with error "object references itself" unless i need to code this way: render :json => #products.to_json.
How to fix this? What happened with render :json => #products?
I've added this to an initializer:
class WillPaginate::Collection
def as_json options={}
{
:total_entries => self.total_entries,
:current_page => self.current_page,
:total_pages => self.total_pages,
:per_page => self.per_page,
:items => super
}
end
end
I have a rails app which I want to make searchable with tenderlove's texticle. In the console it works fine, but in my app I get an error like this:
/opt/local/lib/ruby/gems/1.8/gems/texticle-1.0.3/lib/texticle.rb:65:in `index'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:92:in `call'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:92:in `named_scope'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:97:in `call'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:97:in `search'
/Users/vjmayr/softwareclick/app/controllers/categories_controller.rb:12:in `search'
...
def search
#allproducts = Product.search(params[:search], :conditions => ['category_id in (?)', #category.subtree_ids]) #line 12
if params[:search]
#products = Product.search(params[:search], :conditions => ['category_id in (?)', #category.subtree_ids]).paginate :per_page => 30, :page => params[:page] #line 14
else
#products = []
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #category }
end
end
Can someone point me towards the root of the problem? I am stuck...
Thanks!
Val
P.S. Strange thing about line 12 is, that it shouldn't be used, as I have search in the params .... When I disable it, the error refers to line 14
Try this:
Product.search(params[:search]).all :conditions => ...
# this also works
Product.search(params[:search]).paginate :conditions => ...
I hope it helps.