Main Contents

Using partials in Actionmailer Templates

actionmailer, bugfix, rails

If you try to use partials in an actionmailer template, you probably get the the following error message:

undefined method `controller_path' for SupportMailer:Class

That is because the actionmailer does not support partials out of the box. (here is the belonging Trac Ticket) .

But there are two workarounds:

Patching the actionmailer staticly

This workaround is metioned in the Bug Report so I will describe its usage first, though I don’t like it.

  1. open your ruby directory (C:\Programm files\ruby or /usr/local/ruby) and then navigate to this subdirectory lib\ruby\gems\1.8\gems\actionmailer-1.x.x\lib\action_mailer.
  2. open the file base.rb
  3. add the following line right before/above the private statement:
    # enable partials in ActionMailer templates, there is still a lot to be
    # done to make it work with auto multi-part partials
    def self.controller_path #:nodoc:
      return  ''
    end
  4. Restart your Server

That’s it. Now you can use partials inside your mail templates.

Patching the actionmailer at runtime

This is my prefered way to deal with this “Bug”.

I add  the following file in my rails app under the path config/actionmailer_patch.rb

module ActionMailer

  class Base

    # enable partials in ActionMailer templates, there is still a lot to be
    # done to make it work with auto multi-part partials
    def self.controller_path #:nodoc:
      return  ''
    end
  end
end

Then include this file to your rails app by adding the following line to your config/environment.rb file:

require "actionmailer_patch"

After a restart of your webbrick server/fcgi process you should be able to use partials in ActionMailer Templates.

Comparing the solutions

The first solution has to be executed on every computer you run your app on. After every upgrade of the actionmailer this task has to be redone.

The second solution has to be done for every of your rails apps.

I usally have more mashiens and version sthen apps, that is why I prefer the second solution.

jazzanowak @ March 13, 2008

3 Comments

  1. Peter October 8, 2008 @ 20:35

    Hi,

    great to have found your blog.
    I got not the same exception, but a quite similar one, when I tried to signup with the restful_authentication plugin.
    The strange thing about it is that all unit tests are passing. So in the tests (I debugged it) rails found the ‘perform_delivery_smpt’ method.
    I tried the proposed fix, but it does not help. Do you have an idea?

    Regards,
    Peter.

    Exception:
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `__send__’
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `deliver!’
    /usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:383:in `method_missing’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/observer.rb:171:in `send’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/observer.rb:171:in `update’
    /usr/lib/ruby/1.8/observer.rb:185:in `notify_observers’
    /usr/lib/ruby/1.8/observer.rb:184:in `each’
    /usr/lib/ruby/1.8/observer.rb:184:in `notify_observers’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/callbacks.rb:309:in `notify’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/callbacks.rb:296:in `callback’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/callbacks.rb:221:in `create_without_timestamps’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/timestamp.rb:29:in `create’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:2483:in `create_or_update_without_callbacks’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/callbacks.rb:207:in `create_or_update’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:2211:in `save_without_validation’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/validations.rb:911:in `save_without_dirty’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/dirty.rb:75:in `save_without_transactions’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/transactions.rb:106:in `save’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/transactions.rb:79:in `transaction’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/transactions.rb:98:in `transaction’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/transactions.rb:106:in `save’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/transactions.rb:118:in `rollback_active_record_state!’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/transactions.rb:106:in `save’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:106:in `call’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:106:in `respond_to’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:1166:in `send’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:1166:in `perform_action_without_filters’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:579:in `call_filters’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:572:in `perform_action_without_benchmark’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/rescue.rb:201:in `perform_action_without_caching’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/caching/sql_cache.rb:13:in `perform_action’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache’
    /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/query_cache.rb:8:in `cache’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/caching/sql_cache.rb:12:in `perform_action’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:529:in `send’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:529:in `process_without_filters’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:568:in `process_without_session_management_support’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/session_management.rb:130:in `process’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:389:in `process’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:149:in `handle_request’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:107:in `dispatch’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:104:in `synchronize’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:104:in `dispatch’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:120:in `dispatch_cgi’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:35:in `dispatch’
    /usr/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/webrick_server.rb:112:in `handle_dispatch’
    /usr/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/webrick_server.rb:78:in `service’
    /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service’
    /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’
    /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread’
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start’
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread’
    /usr/lib/ruby/1.8/webrick/server.rb:95:in `start’
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `each’
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `start’
    /usr/lib/ruby/1.8/webrick/server.rb:23:in `start’
    /usr/lib/ruby/1.8/webrick/server.rb:82:in `start’
    /usr/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/webrick_server.rb:62:in `dispatch’
    /usr/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/commands/servers/webrick.rb:66
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:510:in `require’
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:355:in `new_constants_in’
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:510:in `require’
    /usr/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/commands/server.rb:39
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    script/server:3

    Request

    Parameters:

    {”user”=>{”password_confirmation”=>”123456″,
    “login”=>”peter”,
    “password”=>”123456″,
    “email”=>”email@email.de”},
    “commit”=>”Registrieren”,
    “authenticity_token”=>”10883534d7de15c443c61c9ccf804a480a4bde8b”}

  2. Peter October 8, 2008 @ 20:40

    Ups, missed the important lines:
    action_mailer/base.rb:508:in `__send__’
    action_mailer/base.rb:508:in `deliver!’
    action_mailer/base.rb:383:in `method_missing’
    app/models/user_observer.rb:4:in `after_create’
    active_record/observer.rb:171:in `send’
    observer.rb:171:in `update’

    My UserObserver calls UserMailer.deliver_signup_notification(user)
    which then calls the ActionMailer methods.

  3. Recent Faves Tagged With "actionmailer" : MyNetFaves October 10, 2008 @ 12:05

    […] public links >> actionmailer Using partials in Actionmailer Templates First saved by piazzeus | 1 days ago ActionMailer split settings First saved by rhusar | 29 […]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Feed