using helper methods in email templates


I'm creating an template for an html email. My own helper method
'format_price' is not accepted, but it works in some other templates.


# Methods added to this helper will be available to all templates in
the application.
module ApplicationHelper
  def format_price(amount)
    euro, cents = amount.divmod(100)
    sprintf("EUR %d,%02d" , euro, cents)


class OrderMailer < ActionMailer::Base
  def confirmation(order)
    @subject = ''
    @body = { :order => order }
    @recipients =
    @from = ""
    @sent_on =
    @headers = {}

private method in the controller:

  def confirm_order(order)
    email = OrderMailer.create_confirmation(order) # line 185
    render(:text => "<pre>" + email.encoded + "</pre>" )

the error message:

NoMethodError in Cart_and_orders#new_order
Showing app/views/order_mailer/confirmation.rhtml where line #23

undefined method `format_price' for #<ActionView::Base:0x47ca250>

Extracted source (around line #23):

20: <td><%= order_item.times %>&times;</td>
21: <td><%= h(order_item.product.title) %></td>
22: <td></td>
23: <td align="right"><%=
format_price(order_item.product.price) %></td>
24: <td align="right"><%=
format_price(order_item.product.price * order_item.times) %></td>
25: </tr>
26: <% end %>

RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace

Add a reference to your helper in your OrderMailer ActionMailer class,
like this:

helper :application

That should solve your problem (at least this is what I do to fix it).


Dave - affordable VPS Hosting - web based SSH