kaminari: Undefined method safe_append

Kaminari master, rails 5. I have an error.

ActionView::Template::Error (undefined method `safe_append=' for "":ActiveSupport::SafeBuffer):
     6:     remote:        data-remote
     7:     paginator:     the paginator that renders the pagination tags inside
     8: -%>
     9: <%= paginator.render do -%>
    10:   <nav class="pagination">
   21:     def paginate(scope, options = {})
=> 22:       paginator = Kaminari::Helpers::Paginator.new(self, options.reverse_merge(:current_page => scope.current_page, :total_pages => scope.total_pages, :per_page => scope.limit_value, :remote => false))
   23:       paginator.to_s
   24:     end

It will render if you change output_buffer like that before to_s

    def paginate(scope, options = {})
      paginator = Kaminari::Helpers::Paginator.new(self, options.reverse_merge(:current_page => scope.current_page, :total_pages => scope.total_pages, :per_page => scope.limit_value, :remote => false))
      paginator.output_buffer = ActionView::OutputBuffer.new
      paginator.to_s
    end

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 6
  • Comments: 18 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I’m not sure of the cause, but here is some basic information I’ve gathered:

  • Rails has SafeBuffer and OutputBuffer. SafeBuffer provides a safe_concat method. OutputBuffer inherits from SafeBuffer. OutputBuffer aliases safe_concat as safe_append, but SafeBuffer does not.
  • Issue #11 was concerned with instantiating a buffer when one was not already intialized by the template. The code for this fix has, I think, migrated to Paginator#initialize. Specifically: @output_buffer = template.output_buffer.class.new.

The implication of these two points, and the monkey patches in this thread, is that a SafeBuffer is being instantiated where an OutputBuffer should be. However, I can’t see where that is. From a brief code search, HAML seems to choose OutputBuffer.

It is also unclear to me where the call to safe_append occurs. safe_append doesn’t appear in the kaminari or HAML code base. It’s only invoked in the Rails code base in the context of ERB templates, which neither kaminari nor HAML delegate to, that I can see.

I have the same problem, but unfortunately my code base is not public so I cannot share it.

However, here’s a handy copy-pasta monkey patch for anyone needing it…

# put somethwere, like #{RAILSROOT}/config/initializers/kaminari_monkey_patch.rb
module Kaminari
  module ActionViewExtension
    def paginate(scope, options = {})
      paginator = Kaminari::Helpers::Paginator.new(self, options.reverse_merge(:current_page => scope.current_page, :total_pages => scope.total_pages, :per_page => scope.limit_value, :remote => false))
      paginator.output_buffer = ActionView::OutputBuffer.new
      paginator.to_s
    end
  end
end

Thanks for the fix @onemanstartup I ran into the same problem