Generate PDF by binding erb template that contains instance variable while sending email
<p>While sending a mail, I need to convert the mail as a pdf document.</p>

<p>In order to do this i need to bind the erb template with the data, but the erb template contains instance variables</p>

<p>let the template be..,</p>

<pre><code>&lt;h1&gt;Name: &lt;%=@name%&gt;&lt;h1&gt;
&lt;h2&gt;Address: &lt;%= @address %&gt;&lt;h2&gt;
</code></pre>

<p>I have followed this solution mention in the <a href="https://stackoverflow.com/questions/1338960/ruby-templates-how-to-pass-variables-into-inlined-erb">question</a>, Using a namespace for binding the templates with data.</p>

<pre><code>class Namespace
  def initialize(hash)
    hash.each do |key, value|
      singleton_class.send(:define_method, key) { value }
    end 
  end

  def get_binding
    binding
  end
end

ns = Namespace.new(name: 'Joan', address: 'Chennai, India')
ERB.new(template).result(ns.get_binding)
</code></pre>

<p>This works fine for the templates which doesn't contains instance variables.</p>

<p>I need to pass data to the instance variables in the template is there any possibility for doing this.</p>

<p>And I knew a way to solve this problem by assigning the instance variables with the data that we bind ie)</p>

<p>in the template</p>

<pre><code>   &lt;% @name = name %&gt; 
   &lt;% @address = address %&gt;
   &lt;h1&gt;Name: &lt;%=@name%&gt;&lt;h1&gt;
   &lt;h2&gt;Address: &lt;%= @address %&gt;&lt;h2&gt;
</code></pre>

<p>But I <strong>don't</strong> want this kind of implementation.</p>