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 = "my_email-hi6Y0CQ0nG0@public.gmane.org" @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 => "<pre>" + email.encoded + "</pre>" ) 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: <td><%= order_item.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 #{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'' --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dnstevenson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-24 01:29 UTC
Re: using helper methods in email templates
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 - affordable VPS Hosting http://www.gotossh.com - web based SSH On Jul 23, 8:37 am, Luma <martin....-hi6Y0CQ0nG0@public.gmane.org> wrote:> 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 = "my_em...-hi6Y0CQ0nG0@public.gmane.org" > @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 => "<pre>" + email.encoded + "</pre>" ) > 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: <td><%= order_item.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 > #{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''--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---