I'm trying to use will_paginate and have done everything listed in the github installation/use instructions, but I'm getting a NameError when I try to load the page:
uninitialized constant BooksController::Books
In my Gemfile I have:
gem 'will_paginate', '~> 3.1.0'
In my controller I have:
def index
#paginate books, have 10 per page
#books = Books.paginate :page => params[:page], :per_page => 10
end
And in my index.html.erb, I have added:
<%= will_paginate #books %>
Has anyone encountered this? I've run bundle install, and the gem appeared to be installed.
Use like this #books = Book.paginate :page => params[:page], :per_page => 10
Model name should not be plurals
Related
I'm trying to use a debugger in a homework assignment I'm working on. I installed the gem ruby-debug19, but I can't put it to use. Whenever I put the statement ruby-debug in a method, and I go to the console server it doesn't respond. It's like this:
http://imgur.com/zV1IPka
def index
ruby-debug
#search = params[:search] ? params[:search] : {}
#articles = Article.search_with_pagination(#search, {:page => params[:page], :per_page => this_blog.admin_display_elements})
if request.xhr?
render :partial => 'article_list', :locals => { :articles => #articles }
else
#article = Article.new(params[:article])
end
end
Any help would be really appreciated. Thanks.
If you want something flexible with the option of setting breakpoints and navigating through code I can recommend pry-debugger. If you want something with glitter and magic jazz_hands is the gem you are looking for.
def index
#users = User.order("name").page(params[:page]).per_page(5)
end
This is my UsersController where i try to view all users from database. Problem is because is see only this error:
undefined method `page' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x000000044853d8>
Where is the problem?
You can do like this incase you have used will_paginate gem
#users = User.order("name ASC").paginate(:page => params[:page], :per_page => 5)
Take a look on the gem will_paginate
The usage of will_paginate is slight different than Kaminari. You should do:
#users = User.order('name').paginate(page: params[:page], per_page: 5)
Writing sample_app for railstutorial and having next error.
I've added <%= will_paginate #microposts %> to the user profile View and #microposts = #user.microposts.paginate(:page => params[:page]) to UsersController
> NoMethodError (protected method `wp_parse_options' called for #<Class:0x007fde66015a80>):
app/controllers/users_controller.rb:17:in `show'
Where is the problem?
Try gem 'will_paginate', '~> 3.0' and bundle install again, restart server. Give paginate some extra params just for the hell of it paginate(page: params[:page], per_page: 15)
So I am running a rails 2.3.11 app, with bundler and I am having the darndest time getting will_paginate to work. Its returning the following error:
undefined method `paginate' for #<Class:...
def show
#category = Category.find_by_url_name(params[:id])
#brands = #category.brands.find(:all, :order => "name")
#categories = Category.find(:all)
#meta_title = "#{#category.name}"
respond_to do |format|
format.html do |wants|
#brand = #brands.first
#products = Product.paginate(:page => params[:page], :per_page => 6, :conditions => ['brand_id = ? AND category_id = ?', #brand.id, #category.id])
render :template => 'brands/show'
end
format.xml { render :xml => #category }
end
end
this is my controller action. the full error:
NoMethodError in CategoriesController#show
undefined method `paginate' for #<Class:0x1034703e8>
RAILS_ROOT: /Users/tjs/Sites/emeraldcg
Application Trace | Framework Trace | Full Trace
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-2.3.11/lib/active_record/base.rb:1998:in `method_missing'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:28:in `show'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:25:in `show'
Request
Parameters:
{"id"=>"acoustic-guitars"}
Show session dump
Response
Headers:
{"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache"}
my gemfile is pretty straight forward:
# A sample Gemfile
source "http://rubygems.org"
# gem "rails"
gem "rails", '2.3.11'
gem "rake"
gem "RedCloth", ">=4.2.2"
gem "will_paginate", "~> 2.3.16"
group :development do
gem "mysql"
gem "capistrano"
gem "capistrano-ext"
end
group :test do
gem "sqlite3"
end
I would understand if this failed due to it being an array but why is it failing as a class method?
thanks for the help.
In the past there where some problems with will_paginate and Rails 2, try to update to the latest version op will_paginate. In the meanwhile you can try this :
#products = Product.all.paginate(:page => params[:page], :per_page => 6, :conditions => ['brand_id = ? AND category_id = ?', #brand.id, #category.id])
You cannot call paginate on a model. It can be called on collections like hashes, array, ActiveRecord.
I have the will_paginate plugin working in an application, but when I paginate a resource it just spits out the HTML as text, doesn't provide links to the next pages and such.
And when I manually type in the URL the plugin is working it just doesn't make <%= will_paginate #products %> into links such as next 1 2 3 ... last
This is the output
<span class="disabled prev_page">« Previous</span> <span class="current">1</span> 2 Next »
controller:
def index
#products = Product.all.paginate :per_page => 5, :page => params[:page]
#product_categories = ProductCategory.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
end
view
<%= will_paginate #products %>
<%= will_paginate %> #for some reasons this works too
source 'http://rubygems.org'
gem 'rails', '3.0.0.beta2'
gem "will_paginate", '3.0.pre'
if you run into troubles related to haml we use that version:
gem 'haml', '3.0.2'
will_paginate is now at this location:
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => "rails3"
update your gemfile
I believe the reasons is the ways rails3 escapes html and for whatever reason will_pagiante is getting escaped.
to fix this you first need to get the correct gem as the plugin won't work so add gem 'agnostic-will_paginate', :require => 'will_paginate' and that is done in the new gem file located in the app folder of a rails3 project.
After that you need to stop rails from escaping will_paginate with raw so something like
<%=raw will_paginate #products %> which is the opposition of <%=h will_paginate #products %> which in rails3 is equivalent to <%= will_paginate #products %>
WILL PAGINATE MOVED TO GITHUB. This repository is no longer updated.
It's recommended that you install the gem instead of a Rails plugin:
gem install will_paginate
and Try Again