2011-10-21 21 views
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?

回答