undefined method `will_paginate', Rails 3.1 / DataMapper

筅森魡賤 提交于 2019-12-20 03:09:17

问题


Newly installed will_paginate 3.0.0

gem 'will_paginate', '~> 3.0.0', :require=>'will_paginate/data_mapper'

Running a controller query:

@tickets = Ticket.paginate(:page => params[:page], :per_page => 5,:username => @ticket.username)

Which works, pulls up all the tickets for a user and paginates in 5's if I put ?page=X where x is a page number in the url.

=will_paginate(@tickets) 

in the view does not work, this results in

undefined method `will_paginate' for #<#<Class:0x000000053674c8>:0x0000000535cd48>

So will_paginate works, but not the view helper. Am I missing something? I'm using slim templating if that makes any difference. Is there some syntax change I'm missing? The documentation is simple but unhelpful beyond this point. I looked into the source, and there does not seem to be any changes, but I cannot figure why it is inaccessible

And then on tangent, this messes with an association.

=> @instance.model_belonging_to_instance.create(:text=>'test')

TypeError: can't convert nil into Integer
    from /home/qx/.rvm/gems/ruby-1.9.2-p180/gems/will_paginate-3.0.0/lib/will_paginate/page_number.rb:16:in `Integer'
    etc etc et al

SOLUTION:

gemfile:

gem 'will_paginate', '~> 3.0.0'       # removed this, :require=>'will_paginate/data_mapper'

intializer:

require 'will_paginate'
require 'will_paginate/data_mapper'

It shows up, but if not at the top of the template, I get a

stack level too deep

error I am unable to interpret


回答1:


Don't use the :require option in the Gemfile, as you already figured out; instead require "will_paginate/data_mapper" somewhere in config/application.rb, for instance after the Bundler setup.




回答2:


There is a similar question with an answer that indicates that auto-requiring here is the problem. See will_paginate undefined method. The Will_paginate gem appears to work though for the question and answer.




回答3:


gem 'will_paginate', '~> 3.0.0', require: %w[
  will_paginate
  will_paginate/data_mapper
]


来源:https://stackoverflow.com/questions/7418061/undefined-method-will-paginate-rails-3-1-datamapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!