# Using helper methods in email templates

**URL:** <https://www.ruby-forum.com/t/using-helper-methods-in-email-templates/110128>\
**Category:** Rails\
**Created:** [July 23, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/using-helper-methods-in-email-templates/110128 "2007-07-23T16:39:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d6d6ee/32.png) [@luma](https://www.ruby-forum.com/u/luma)\
**Post date:** [July 23, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/using-helper-methods-in-email-templates/110128/1 "2007-07-23T16:39:11Z")

</div>

hi

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.

application\_helper.rb:

# 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)  
end  
end

order\_mailer.rb:

class OrderMailer \< ActionMailer::Base  
def confirmation(order)  
@subject = ‘’  
@body = { :order =\> order }  
@recipients = order.customer.email  
@from = “removed\_email\_address@domain.invalid”  
@sent\_on = Time.now  
@headers = {}  
end  
end

private method in the controller:

def confirm\_order(order)  
email = OrderMailer.create\_confirmation(order) # line 185  
email.set\_content\_type(“text/html”)  
render(:text =\> “

```
” + email.encoded + “
```
” )  
end

the error message:

NoMethodError in Cart\_and\_orders#new\_order  
Showing app/views/order\_mailer/confirmation.rhtml where line #23  
raised:

undefined method `format\_price’ for #ActionView::Base:0x47ca250

Extracted source (around line #23):

20:

\<%= order\_item.times %\>×  
21: \<%= h(order\_item.product.title) %\>  
22:   
23: \<%=  
format\_price(order\_item.product.price) %\>  
24: \<%=  
format\_price(order\_item.product.price \* order\_item.times) %\>  
25:   
26: \<% end %\>

RAILS\_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace  
#{RAILS\_ROOT}/app/views/order\_mailer/confirmation.rhtml:23:in  
`_run_rhtml_47app47views47order_mailer47confirmation46rhtml' #{RAILS_ROOT}/app/views/order_mailer/confirmation.rhtml:18:in`\_run\_rhtml\_47app47views47order\_mailer47confirmation46rhtml’  
#{RAILS\_ROOT}/app/controllers/cart\_and\_orders\_controller.rb:185:in  
`confirm_order' #{RAILS_ROOT}/app/controllers/cart_and_orders_controller.rb:155:in`new\_order’  
#{RAILS\_ROOT}/app/controllers/cart\_and\_orders\_controller.rb:151:in  
`new\_order’

---

<div class="post-metadata">

**Author:** ![luma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d6d6ee/32.png) [@luma](https://www.ruby-forum.com/u/luma)\
**Post date:** [July 24, 2007, 3:30am UTC](https://www.ruby-forum.com/t/using-helper-methods-in-email-templates/110128/2 "2007-07-24T03:30:08Z")

</div>

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).

Regards,

Dave  
[http://www.stevensonsoftware.com](http://www.stevensonsoftware.com) - affordable VPS Hosting  
[http://www.gotossh.com](http://www.gotossh.com) - web based SSH
