prikha
prikha

Reputation: 1853

ERB template can`t get instance variables from the controller action:

I have the following problem which ripped my soul for the past 2 weeks:

I want to create reports with pre-rendered 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

following the ERB documentation. But inside the template file DEFAULT.HTML.ERB it sems like i only can see local variables like

 content_for_prerendering = Report.get_content

not instance variables. it is very unusual to bind every instance variable to local just to render some html. Am i missing something?

Upvotes: 0

Views: 964

Answers (1)

Viktor Trón
Viktor Trón

Reputation: 8884

try this to get view context binding to delegate instance vars correctly

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

Upvotes: 1

Related Questions