ruby-on-railsruby-on-rails-3rubygemsactionmailerrails-3-upgrade

rails 3 upgrade - You're using the old API in a mailer class


I'm trying to upgrade an application from rails 2.3 to 3.0.6

which has the following code in rails 2.3

class MessageSender < ActionMailer::Base
    def send_message(subject,to,from,body,respondent = nil, content_type = 'text/plain')
      @content_type        = content_type
      @subject             = subject
      @recipients          = to
      @from                = from
     # @sent_on             = Time.now
      @body                = {:body_text => body}
    end
end

In the upgrade process the code is modified as below

class MessageSender < ActionMailer::Base
    def send_message(subjet,to,from,body,respondent = nil,content_type='text/plain')
      mail(:to => to, :subject => subject, :from => from, :body => body, :content_type => content_type)
    end
end

by referring to this famous blog about using ActionMailer in rails 3.0

And finally ran rake rails:upgrade:check(checks for rails 3 incompatible functions) and it shows

Old ActionMailer class API
You're using the old API in a mailer class.
More information: http://lindsaar.net/2010/1/26/new-actionmailer-api

The culprits: 
        - app/models/message_sender.rb

(i.e) It says I'm still using Old API

Can somebody explain what am I missing here ?

Or is there any other way to eliminate "You're using the old API in a mailer class" bug ?

FYI: the gems are updated and environment is ruby 1.8.7,rails 3.0.6


Solution

  • Try throwing your code away and write it again using ActionMailer guide. The reason can be as Frederick suggested but also your code doesn't look very rails 3 way ;).

    The first thing that comes to my mind is how you pass body and content type. Body can be just variable which you will use in view and content type will be set automatically based on what views are defined.

    I would write something like:

    class MessageSender < ActionMailer::Base
      def send_message(subject, to, from, body)
        # @sent_on           = Time.now
        @body                = body
        mail(:to => to, :subject => subject, :from => from)
      end
    end
    

    Then to render a view:

    # app/views/message_sender/send_message.text.erb 
    
    My nice text email.
    And my body is <%= @body %>
    

    As you can read in the guide, you can also create html version as app/views/message_sender/send_message.text.erb.