mislav / will_paginate

Pagination library for Rails and other Ruby applications
http://github.com/mislav/will_paginate/wikis
MIT License
5.71k stars 864 forks source link

Rails 6.1 with page_entries_info bug #618

Closed kvokka closed 3 years ago

kvokka commented 3 years ago

Using Rails 6.1.rc1, CRuby 2.5.8, will-paginate 3.3.0

page_entries_info(@messages, model: UserMessage, html: false) # => !! #<NoMethodError: undefined method `[]' for nil:NilClass>

backtrace:

gems/will_paginate-3.3.0/lib/will_paginate/view_helpers.rb:128:in `block in page_entries_info': undefined method `[]' for nil:NilClass (ActionView::Template::Error)
    from gems/i18n-1.8.5/lib/i18n/interpolate/ruby.rb:33:in `block in interpolate_hash'
    from gems/i18n-1.8.5/lib/i18n/interpolate/ruby.rb:23:in `gsub'
    from gems/i18n-1.8.5/lib/i18n/interpolate/ruby.rb:23:in `interpolate_hash'
    from gems/i18n-1.8.5/lib/i18n/interpolate/ruby.rb:19:in `interpolate'
    from gems/i18n-1.8.5/lib/i18n/backend/base.rb:188:in `interpolate'
    from gems/i18n-1.8.5/lib/i18n/backend/base.rb:61:in `translate'
    from gems/i18n-1.8.5/lib/i18n/backend/fallbacks.rb:48:in `block (2 levels) in translate'
    from gems/i18n-1.8.5/lib/i18n/backend/fallbacks.rb:47:in `catch'
    from gems/i18n-1.8.5/lib/i18n/backend/fallbacks.rb:47:in `block in translate'
    from gems/i18n-1.8.5/lib/i18n/backend/fallbacks.rb:45:in `each'
    from gems/i18n-1.8.5/lib/i18n/backend/fallbacks.rb:45:in `translate'
    from gems/i18n-1.8.5/lib/i18n.rb:206:in `block in translate'
    from gems/i18n-1.8.5/lib/i18n.rb:202:in `catch'
    from gems/i18n-1.8.5/lib/i18n.rb:202:in `translate'
    from gems/actionview-6.1.0.rc1/lib/action_view/helpers/translation_helper.rb:93:in `translate'
    from app/helpers/application_helper.rb:221:in `translate'
    from gems/will_paginate-3.3.0/lib/will_paginate/view_helpers/action_view.rb:83:in `will_paginate_translate'
    from gems/will_paginate-3.3.0/lib/will_paginate/view_helpers.rb:155:in `page_entries_info'
    from gems/will_paginate-3.3.0/lib/will_paginate/view_helpers/action_view.rb:40:in `page_entries_info'
    from app/views/messages/index.html.haml:6:in `_app_views_messages_index_html_haml__3160112440264369952_69917224413540'
    from gems/actionview-6.1.0.rc1/lib/action_view/base.rb:247:in `public_send'
jonknapp commented 3 years ago

FWIW I made my own helper to fix this in Rails 6.1 till a fix gets a release:

def page_entries_info(collection)
    model_name = collection.respond_to?(:human_name) ? collection.model_name.human : (collection.first&.model_name&.human || '')

    sanitize "Displaying #{model_name} " +
      tag.b("#{collection.offset + 1} - #{[collection.per_page * collection.current_page, collection.total_entries].min}") +
      ' of ' + tag.b(collection.total_entries) +
      ' in total'
end
ansonhoyt commented 3 years ago

@jonknapp thanks for sharing a simple solution. This won't work in many scenarios since it bypasses I18n and single/multi page handling. Another workaround would be to apply https://github.com/mislav/will_paginate/pull/619 locally in an initializer.

We actually just updated our Gemfile to use kvokka's patched branch while we watch for it to be merged. It seems to work fine.

mislav commented 3 years ago

Thank you for reporting! This bug is not in will_paginate, but due to a regression in Rails 6.1 translate method, and will be fixed with Rails 6.1.2: https://github.com/rails/rails/pull/40691

jonknapp commented 3 years ago

It looks like it missed 6.1.2 and 6.1.3; still not merged.

mislav commented 3 years ago

It's now merged but we will have to wait until Rails 6.1.4.

xuanxu commented 3 years ago

Rails 6.1.4 has been released fixing this problem.