我需要创建一些html文件,并希望使用我的部分已经做了显示视图。我曾经使用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方法上给出了一个错误。有什么方法可以告诉它应该使用的render方法是正常的render方法吗?
好了!
发布于 2013-05-07 20:03:25
我找到了其他的解决方案来做到这一点。当我们已经在rails中并且可以使用render本身时,我没有使用Haml::Engine,而是在模型中创建了一个to_html函数,并包含了应用程序帮助器来获取我在部分中使用的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代码片段。
发布于 2017-10-10 08:28:31
你有一个拼写错误- locals =>应该是locals =
https://stackoverflow.com/questions/16403966
复制相似问题