Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `output_buffer=' for content_tag with block

in lib/my_lib.rb

class MyLib
    include ActionView::Helpers::TagHelper

    def foo
      content_tag :div do
         "hello"
       end
    end
end

gives a

require 'lib/my_lib' ;MyLib.new.foo
NoMethodError: undefined method `output_buffer=' for #<MyLib:0x7f3209aaa450>
    from /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/capture_helper.rb:175:in `with_output_buffer'
    from /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/capture_helper.rb:40:in `capture'
    from /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/tag_helper.rb:77:in `content_tag'
    from ./lib/my_lib.rb:6:in `foo'
    from (irb):1
like image 847
Oleg German Avatar asked Mar 27 '11 21:03

Oleg German


1 Answers

Is there a particular reason you can't put this logic inside a view?

content_tag was made for the View environment, and was not intended to be called from within a controller or other library. You will need to set up your MyLib class as a view.

While I recommend that you re-think, and re-smell your code and design that has brought you to the point of needing this, you can accomplish what you want by subclassing from ActionView::Base.

class MyLib < ActionView::Base
  include ActionView::Helpers::TagHelper

  def foo
    content_tag :div do
      "hello"
    end
  end
end
like image 87
Christopher Maujean Avatar answered Oct 26 '22 12:10

Christopher Maujean