Undefined method `paginate' - ruby-on-rails

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.

Related

Basic usage of Jbuilder in a controller

In a Rails (5.2) app, I'm trying to use JBuilder to return some JSON as response.
I have added JBuilder in my Gemfile.
# Gemfile
...
gem 'jbuilder', '~> 2.5'
...
# Gemfile.lock
...
jbuilder (2.8.0)
...
According to JBuilder documentation:
You can also extract attributes from array directly.
#people = People.all
json.array! #people, :id, :name
=> [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
Now, in my controller, I have added the following:
def index
respond_to do |format|
format.json { render json.array! User.all, :email, :full_name }
end
end
But I get
NameError - undefined local variable or method `json' for
UsersController:0x00007fe2f966f150 16:55:40 rails.1
| => Did you mean? JSON:
Am I missing anything here?
You typically use jbuilder in a view file with the extension .json.jbuilder
in your controller:
def index
#users = User.all
respond_to do |format|
format.json
end
end
in your app/views/users/index.json.jbuilder
json.array! #users, :email, :full_name
EDIT: you can do it from the controller like that as well:
format.json { render json: Jbuilder.new { |json| json.array! User.all, :email, :full_name }.target! }
You can do:
def index
Jbuilder.new do |json|
json.array! User.all, :email, :full_name
end.attributes!
end
(It worked for me)
Source
Side note
You might see people doing:
Jbuilder.encode do |json|
# ...
end
But Jbuilder.encode actually returns a string, as specified by this comment in the source code

Kaminari gem not working with Leaderboard gem

I have been following this tutorial to get the leaderboard gem working and I've got all of the leaderboard gem aspect of it working, but I am struggling to get the Kaminari gem part of it working (the paginate gem).
At the moment in my controller I have this:
class LeaderboardController < ApplicationController
before_action :query_options
def show
#lb = Boards.default_leaderboard
#entries = entry_service.execute(query_options)
respond_to do |format|
format.html do
paginate
end
format.json do
render json: #entries
end
end
end
private
def query_options
#limit = [params.fetch(:limit, 10).to_i, 100].min
#page = params.fetch(:page, 1).to_i
{ page: #page, limit: #limit }
end
def paginate
pager = Kaminari.paginate_array(
#entries,
total_count: #lb.total_members)
#page_array = pager.page(#page).per(#limit)
end
And in the views I have:
= paginate #page_array
But for some reason this is not paginating the #entries...
For the way you have it set up you need to call the before_action :paginate.

kaminari pagination error undefined method

this is my post_controller
def index
#posts = Post.all.page(params[:page]).per(2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
this is /views/posts/index
<%= paginate #posts %>
and i recive:
NoMethodError in PostsController#index
undefined method `page' for #
I saw many themes with the same error and solution:
Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
but where should i put this?
You can put it in your controller
#posts = Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
But you also could do this:
#posts = Post.page(params[:page]).per(2)

Rails 3.1: Jbuilder rendering empty views

Trying to use Jbuilder to create some JSON views for my app, but the views are coming out empty. Using the builder code in the console, however, works just fine.
Controller code:
#placements = Placement.all
respond_to do |format|
format.html
format.json
end
Jbuilder view (index.json.builder):
Jbuilder.encode do |json|
json.array!(#placements) do |json, placement|
json.id placement.id
json.name placement.name
end
end
Visiting http://localhost:3000/placements.json results in an empty page. Removing the respond_to format block doesn't help. If I use the following code in my controller, I get an output, but it's obviously not the Jbuilder output.
respond_to do |format|
format.html
format.json {render json: #placements}
end
Has anyone else seen this problem?
#Robin - It was rendering the wrong template, but I had the wrong extension. I was using builder instead of jbuilder.
I think you need to remove f
respond_to do |format|
format.html
format.json {render json: #placements}
end
Same issue in rails specs with jbuilder 2.6.0 rails 5.0.0 and existed views.
Suggested in mentioned issue solutions:
Solved by adding render type with collection or resource
for example for index action replace:
render :index
with:
render :index, json: #collection
Or solved by configuring rspec like this:
# spec/spec_helper.rb
RSpec.configure do |config|
config.render_views = true
end
Or
class ApplicationController < ActionController::API
include ActionView::Rendering
end

paginate in rails 2.3.8

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. :)

Resources