[英]rails 3 upgrade - You're using the old API in a mailer class
我正在尝试将应用程序从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
通过参考有关在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
(即)它说我还在使用旧API
有人可以解释一下我在这里想念什么吗?
还是有其他方法可以消除“您在邮件程序类中使用旧API”的错误?
仅供参考:宝石已更新且环境为红宝石1.8.7,轨道为3.0.6
尝试扔掉代码,然后使用ActionMailer guide再次编写。 原因可能是Frederick所建议的,但是您的代码看起来也不是3种方式;)。
我想到的第一件事是您如何传递正文和内容类型。 主体可以只是您将在视图中使用的变量,并且内容类型将根据定义的视图自动设置。
我会写类似:
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
。
Rails升级通过对其运行一些正则表达式来检查您的代码。 这不是万无一失的,例如,它试图防止采用旧的方式设置主题:
subject "foo"
但是用于检查该内容的测试将捕获主题词一词的任何实例,后跟一个空格(用作符号时除外)。 由于您有一个参数调用subject
这很容易发生。 from
也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.