trailblazer / cells

View components for Ruby and Rails.
https://trailblazer.to/2.1/docs/cells.html
3.07k stars 236 forks source link

undefined method `encoding' for []:Array in ERB template (4.0.0beta2) #275

Closed samstickland closed 9 years ago

samstickland commented 9 years ago

This is similar to issue #265 but for ERB templates.

The error is raised in ActionView:Helpers:Capture:Helper

  def with_output_buffer(buf = nil) #:nodoc:
    unless buf
      buf = ActionView::OutputBuffer.new
      buf.force_encoding(output_buffer.encoding) if output_buffer
    end
    self.output_buffer, old_buffer = buf, output_buffer
    yield
    output_buffer
  ensure
    self.output_buffer = old_buffer
  end

Something has already defined output_buffer as an empty array, which makes it die here. Digging into the cells code shows that it's this method in ViewModel:

def output_buffer
  @output_buffer ||= []
end
attr_writer :output_buffer # TODO: test that, this breaks in MM.

This can be fixed by monkey patching like this, but it is bringing back ActionView:

module Cell
  class ViewModel < AbstractController::Base

  private
    def output_buffer
      @output_buffer ||= ActionView::OutputBuffer.new
    end
  end
end

The longer term fix would be defining output_buffer to something that supports all the methods ActionView requires?

seuros commented 9 years ago

add cells-erb gem

samstickland commented 9 years ago

Thanks :) That really ought to be mentioned in the readme (it currently says: Cells comes bundled with ERB support. To render HAML, you have to include the cells-haml gem. The same for cells-slim).

Unfortunately it didn't work. I installed cells-erb (0.0.1) and without my monkey patch it still gave the same error message.

apotonick commented 9 years ago

@seuros This is because we removed many tests when I merged the gigantic PR. We have to re-add those tests before we release Cells 4.

apotonick commented 9 years ago

This is fixed in cells-erb 0.0.2.