will_paginate gem problem
So I'm trying to use will_paginate-rails3 and its not working out here's what I have in my controller:
def index
@posts = Post.paginate(:page => params[:page], :per_page => 2 )
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
Gemfile: gem "will_paginate-rails3", '~> 3.0.pre'
posts/index.html.erb: <%= will_paginate @posts %>
NoMethodError: NoMethodError (undefined method `paginate' for #<Class:0x12c3438>):
app/controllers/posts_controller.rb:5:in `index'
Rendered C:/Ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
Rendered C:/Ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/Ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.6ms)
开发者_如何学编程
Bundle install says the gem is there:
Anybody know where I made my mistake? Thanks,
Matt
Try using
def index
@posts = Post.all.paginate(:page => params[:page], :per_page => 2 )
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
I use just
gem 'will_paginate'
in my Gemfile and it works
Try putting this in your Gemfile instead:
gem "will_paginate", "~> 3.0.pre2"
Try to delete the "all". I had the same problem, since I deleted the "all", it works now. My code used to look like this : .all.paginate :per_page => events_per_page, :page => params[:page] delete the "all" before paginate. Hope it works for you. :)
精彩评论