Rails 5:为什么分页无法正常工作?

时间:2019-04-12 19:03:01

标签: ruby-on-rails ruby-on-rails-5 will-paginate

我添加了gem,重新启动了服务器。

在索引操作中,我有:

def index
  @listings = Listing.paginate(page: params[:page])
end

我重新加载视图。可以。

然后我打开分页链接:

<%= will_paginate @listings %>

我得到一个错误:

ActionView::Template::Error (wrong number of arguments (given 0, expected 1)):
    12: 
    13: <%= render 'shared/subscribe' %>
    14: 
    15: <%= will_paginate @listings %>
    16: <div class="content listings">
    17:   <h1>Latest<span class="meta">updated <%= time_ago_in_words(@listings.first.updated_at) %> ago</span></h1>
    18:   <% @listings.each do |listing| %>

app/views/listings/index.html.erb:15:in `_app_views_listings_index_html_erb___4466103937708592830_70228331044140'

由于某些原因,<%= will_paginate @listings %>无法正常工作。

在错误控制台中,我可以看到错误源自:

Listing.paginate(page: params[:page])

完整错误是:

ArgumentError: wrong number of arguments (given 0, expected 1)
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/active_record.rb:149:in `select_for_count'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/activerecord-5.2.3/lib/active_record/relation/calculations.rb:225:in `perform_calculation'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/activerecord-5.2.3/lib/active_record/relation/calculations.rb:145:in `calculate'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/activerecord-5.2.3/lib/active_record/relation/calculations.rb:52:in `count'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/active_record.rb:97:in `count'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/active_record.rb:95:in `count'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/active_record.rb:79:in `total_entries'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/active_record.rb:135:in `block in to_a'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/collection.rb:96:in `create'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/will_paginate-3.0.12/lib/will_paginate/active_record.rb:133:in `to_a'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/activerecord-5.2.3/lib/active_record/relation/finder_methods.rb:512:in `find_take_with_limit'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/activerecord-5.2.3/lib/active_record/relation/finder_methods.rb:103:in `take'
from /Users/guff/.rvm/gems/ruby-2.6.2/gems/activerecord-5.2.3/lib/active_record/relation.rb:513:in `inspect'

任何人都可以看到发生了什么吗?我很困惑。是什么导致错误?

2 个答案:

答案 0 :(得分:0)

我使用的是will_paginate的过时版本。

升级到3.1.7后问题消失了。

答案 1 :(得分:0)

在Rails 5.2中将will_paginate gem升级到3.1.7消除了提到的错误。