【问题标题】:ERB template can`t get instance variables from the controller action:ERB 模板无法从控制器操作中获取实例变量:
【发布时间】:2012-05-30 20:54:08
【问题描述】:

在过去的 2 周里,我遇到了以下困扰我的问题:

我想用预渲染的 html-body 创建报告:

def new
  @content_for_prerendering= Report.get_content
  @report = Report.new
  template = ERB.new(File.read("#{Rails.root}/app/views/report_template/default.html.erb"))
  @report.body = template.result(binding)
end

遵循 ERB 文档。但在模板文件 DEFAULT.HTML.ERB 中,我似乎只能看到像

这样的局部变量
 content_for_prerendering = Report.get_content

不是实例变量。将每个实例变量绑定到本地只是为了呈现一些 html 是非常不寻常的。我错过了什么吗?

【问题讨论】:

    标签: ruby-on-rails erb


    【解决方案1】:

    试试这个以获得视图上下文绑定以正确委托实例变量

    @report.body = render_to_string :file => "#{Rails.root}/app/views/report_template/default.html.erb"
    

    【讨论】:

    • Ill check it out in a moment! - Did you mean render_to_string` 因为它抱怨render_to_text - 没有方法错误?
    • 这就像魔法一样,但我ve chosen render_to_body. Please correct your answer to something like render_to_string` 或render_to_body
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多