[英]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.