将部分内容中的本地和部分呈现为haml到html文件

时间:2013-05-06 17:29:11

标签: ruby ruby-on-rails-3 haml render

我需要创建一些html文件,并希望将已经完成的部分内容用于show视图。 我以前用markaby这样做,但现在我觉得用haml这个东西会更容易。 我正在尝试:

Haml::Engine.new(File.read("#{Rails.root}/app/views/metric_box_sets/_metric_box_set.html.haml"), 
:format => :html5, :ugly => true).render(Object.new,locals =>{:metric_box_set=>@metric_box_set})

在我正在使用的部分中,我多次访问metric_box_set,但也渲染其他部分并为它们提供与此关联的其他对象。问题是它在render方法上给出了错误。有一种方法可以告诉它应该使用的渲染方法是普通的渲染方法吗?

TNKS!

2 个答案:

答案 0 :(得分:0)

我找到了其他解决方案来做到这一点。 而不是使用Haml :: Engine,一旦我们已经在rails中并且我们可以使用render本身,我在模型中创建了一个to_html函数,并包含应用程序帮助程序以获取我在partial中使用的helper_methods。然后使用渲染将结果打印到文件:

def to_html
  ActionView::Base.send :include, ActionView::Helpers::ApplicationHelper

  File.open(File.join(Rails.root, "public", 'test.html'), "w") do |file|

    file.print ActionView::Base.new(Rails.configuration.paths["app/views"].first).render(
                :partial => 'metric_box_sets/metric_box_set', 
                :format => :html,
                :locals => { :metric_box_set => self}
              )
  end  
end

这样我就可以基于我已经完成的视图生成我需要的所有html片段。

答案 1 :(得分:-1)

你有一个错字 - locals =>应该是locals =