Ruby on rails 未定义的方法“输出缓冲区=';对于带有块的内容\标记
在lib/my_lib.rb中Ruby on rails 未定义的方法“输出缓冲区=';对于带有块的内容\标记,ruby-on-rails,Ruby On Rails,在lib/my_lib.rb中 class MyLib include ActionView::Helpers::TagHelper def foo content_tag :div do "hello" end end end 给出 require 'lib/my_lib' ;MyLib.new.foo NoMethodError: undefined method `output_buffer=' for #<My
class MyLib
include ActionView::Helpers::TagHelper
def foo
content_tag :div do
"hello"
end
end
end
给出
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
需要'lib/my_lib';MyLib.new.foo
NoMethodError:for的未定义方法“output\u buffer=”#
from/var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action\u view/helpers/capture\u helper.rb:175:in'with\u output\u buffer'
from/var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action\u view/helpers/capture\u helper.rb:40:in“capture”
from/var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action\u view/helpers/tag\u helper.rb:77:in'content\u tag'
from./lib/my_lib.rb:6:in'foo'
来自(irb):1
是否有特殊原因使您无法将此逻辑放入视图中
content_标记是为视图环境创建的,不打算从控制器或其他库中调用。您需要将MyLib类设置为视图
虽然我建议您重新思考,重新嗅一嗅代码和设计,使您达到了需要的程度,但您可以通过从ActionView::Base中进行子类化来完成您想要的
class MyLib < ActionView::Base
include ActionView::Helpers::TagHelper
def foo
content_tag :div do
"hello"
end
end
end
class MyLib