2013-07-24 28 views
0

我試圖實現從簡單的Ruby on Rails服務向自己發送通知電子郵件。我一直在使用ActionMailer的指南,在http://guides.rubyonrails.org/action_mailer_basics.html和文檔http://api.rubyonrails.org/classes/ActionMailer/Base.html我基本上從指南複製/粘貼代碼,但是當我嘗試運行它時遇到了一堆語法錯誤。Rails ActionMailer實現中的語法錯誤

我已經四處搜尋,但還沒有遇到任何人有同樣的問題。鑑於這一點以及我對Rails和Ruby的經驗不足,我懷疑我可能會錯過一些根本性的東西。

這是我的郵件:

class NotificationMailer < ActionMailer::Base 
    default from: '[email protected]' 

    def uploadNotification 
    mail(to: "[email protected]", subject: "Upload Notification") 
    end 
end 

,我稱它是這樣的:

NotificationMailer.uploadNotification.send 

當應用程序試圖調用該方法,我得到這些錯誤:

SyntaxError (/Users/jimmcgowan/Sites/RoR/upload/app/models/notification_mailer.rb:2: syntax error, unexpected ':', expecting kEND 
default from: '[email protected]' 
      ^
/Users/jimmcgowan/Sites/RoR/upload/app/models/notification_mailer.rb:5: syntax error, unexpected ':', expecting ')' 
mail(to: "[email protected]", subject: "Upload Notification") 
     ^
/Users/jimmcgowan/Sites/RoR/upload/app/models/notification_mailer.rb:5: syntax error, unexpected ',', expecting kEND 
mail(to: "[email protected]", subject: "Upload Notification") 
          ^
/Users/jimmcgowan/Sites/RoR/upload/app/models/notification_mailer.rb:5: syntax error, unexpected ')', expecting kEND): 

爲了嘗試進行更簡單的測試,我刪除了'default from:'行,取代了uploadNotification方法中的mail()調用使用簡單的日誌,並將調用代碼更改爲NotificationMailer.uploadNotification。但是,這導致了NoMethodError。

任何人都可以給我一些關於我要去哪裏的錯誤嗎?

更新

似乎bgates答案是正確的,這是由語法上的不同版本1.8和Ruby 1.9造成的。然而,升級我的託管虛擬服務器上的Ruby安裝(運行舊版本的Suse)是不切實際的,所以我重寫了該類以符合Ruby 1.8。對於檔案的緣故,這裏的工作版本:

class NotificationMailer < ActionMailer::Base 
    def uploadNotification 
    from "My Server <[email protected]>" 
    recipients "[email protected]" 
    subject "New Notification" 
    end 
end 

我這樣稱呼它:

NotificationMailer.deliver_uploadNotification 
+0

紅寶石從未使用大小寫混合的約定。 'uploadNotification'應改爲'upload_notification' – OneChillDude

+0

重新啓動您的服務器? – OneChillDude

+0

我試過重新啓動服務器,也切換到'upload_notification'作爲方法名,但結果相同。 –

回答

0

它在抱怨以前是用Ruby 1.8.x.語法錯誤的東西如果在命令行鍵入

$ ruby -v 

,並且您得到的數字小於1.9,則需要將Ruby更新爲新版本。

+0

這可能是問題,我有1.8.7。但是由於我在OS X上,看起來升級需要一些工作。 OSX GUI版本的RVM網站似乎目前處於關閉狀態,但是一旦它恢復正常,我將更新Ruby並查看會發生什麼...... –

0

我會做這樣的事情:

class NotificationMailer < ActionMailer::Base 
    default from: "Mydomain <[email protected]>", 
      return_path: "Mydomain <[email protected]>" 

    def upload_notification 
     mail(:to => "[email protected]", 
      :subject => "Upload Notification") 
    end 
end 

,然後用.deliver調用它,而不是

。發送
NotificationMailer.upload_notification.deliver 
+0

我仍然在'default :'行,儘管現在使用mail()調用中的語法就可以了。我會在更新Ruby之後再試一次。 –