我有一个Rails应用程序,我在其中呈现存储在模型属性中的一块Haml内容.在Haml的那个块中使用Rails视图助手会很不错.目前我在视图助手中使用Haml :: Engine#render来呈现此模型属性的内容.它工作得很好,但我不能使用像= link_to这样的东西.为了说明问题:
irb(main):003:0> haml_text=<<"EOH" irb(main):004:0" %p irb(main):005:0" =image_tag 'someimage' irb(main):006:0" EOH => "%p\n =image_tag 'someimage'\n" irb(main):007:0> engine = Haml::Engine.new(haml_text) => #<Haml::Engine:0x7fa9ff7f1150 ... > irb(main):008:0> engine.render NoMethodError: undefined method `image_tag' for #<Object:0x7fa9ff7e9a40> from (haml):2:in `render' from /usr/lib/ruby/gems/1.8/gems/haml-3.0.25/lib/haml/engine.rb:178:in `render' from /usr/lib/ruby/gems/1.8/gems/haml-3.0.25/lib/haml/engine.rb:178:in `instance_eval' from /usr/lib/ruby/gems/1.8/gems/haml-3.0.25/lib/haml/engine.rb:178:in `render' from (irb):8
有关如何做到这一点的任何想法?
好主意?
解决方法
render方法允许您指定上下文.就像是
base = Class.new do include ActionView::Helpers::AssetTagHelper include ApplicationHelper end.new Haml::Engine.new(src).render(base)
可以工作.