未定义的方法`paginate’
我正在尝试使用will_paginate gem,但是出了点问题。 我遇到了一个undefined method `paginate'
错误。 我读了很多问题并尝试了很多东西。 这是我得到的:
这是我的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
这是我在index.html.erb
will_paginate行:
这是错误:
undefined method `paginate' for #
我还在我的Gemfile
添加了require部分:
gem "will_paginate", "~> 3.0.4", :require => nil
这在我的environment.rb
结束时environment.rb
:
require "will_paginate"`
will_paginate不会那样工作。 paginate
是Location
类的一个方法:
def index @locations = Location.paginate(page: params[:page], per_page: 10) respond_to do |format| format.html format.json { render json: @locations } end end
此外,要使用will_paginate,您只需要在Gemfile
添加以下行,不需要在environment.rb
中进行修改:
gem "will_paginate", "~> 3.0.4"
确保在安装'will_paginate' gem
后重新启动服务器。
这是我案中的愚蠢问题。
您尝试对数组进行分页。 试试这个:
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
如果你想分页数组,请参阅使用will_paginate在Ruby中对一个数组进行Paginating