【问题标题】:rails 3 upgrade - You're using the old API in a mailer classrails 3 upgrade - 您在邮件程序类中使用旧 API
【发布时间】:2013-05-01 06:07:42
【问题描述】:

我正在尝试将应用程序从 rails 2.3 升级到 3.0.6

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

升级过程中代码修改如下

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

参考这个著名的blog 关于在rails 3.0 中使用ActionMailer

最后运行rake rails:upgrade:check(检查rails 3 不兼容的功能),它显示

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

(即)它说我仍在使用 Old API

有人能解释一下我在这里缺少什么吗?

或者有没有其他方法可以消除“您在邮件类中使用旧 API”的错误?

仅供参考:宝石已更新,环境为 ruby​​ 1.8.7,rails 3.0.6

【问题讨论】:

  • 也许这是因为您的邮件在app/models 而不是app/mailers
  • @Lucas 尝试将其移至 app/mailers 但结果相同

标签: ruby-on-rails ruby-on-rails-3 gem actionmailer rails-3-upgrade


【解决方案1】:

尝试丢弃您的代码并使用ActionMailer guide 重新编写它。原因可能正如 Frederick 所建议的那样,但您的代码看起来也不像 rails 3 way ;)。

我首先想到的是如何传递正文和内容类型。正文可以只是您将在视图中使用的变量,内容类型将根据定义的视图自动设置。

我会写这样的:

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

然后渲染一个视图:

# app/views/message_sender/send_message.text.erb 

My nice text email.
And my body is <%= @body %>

正如您在指南中所读到的,您还可以将 html 版本创建为 app/views/message_sender/send_message.text.erb

【讨论】:

    【解决方案2】:

    Rails 升级通过运行一些正则表达式来检查您的代码。这不是万无一失的,例如它试图防止设置主题的旧式方式:

    subject "foo"
    

    但用于检查的tests 将捕获单词主题后跟空格的任何实例(用作符号时除外)。由于您有一个参数调用subject,这很容易发生。 from也是如此。

    【讨论】:

    • 谢谢哥们!!我被震撼了..现在我明白为什么会显示一些不需要的文件了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2012-11-22
    • 2011-08-22
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多