1
绑定我用ERB模板到处使用以下方法来使用:在1.9.2
def create_from_template(file, template, vars)
contents = File.read(template(template))
struct = OpenStruct.new(vars)
result = ERB.new(contents).result(struct.binding)
File.open(file, 'w') { |f| f.write result }
end
我用它的方式如下:
app = App.new(...)
create_from_template('new_file', 'template', { :app => app })
所以在我的模板,我可以有:
<%= app.name %>
而且,它还将在REE 1.8.7在1.9.2我收到以下错误代替罚款,但是:
NameError:
undefined local variable or method `app' for #<ERB:0x007ffe6b838468>
问题:如何让此代码符合1.9.2?