Ruby 板


LINE

吧,对不起推文推太长了... ※ 引述《prelight (失败者)》之铭言: : 环境:Ruby 1.8.6 Rails 2.3.2 : : 状况:依照这一篇文章http://0rz.tw/MzM5Q去实做,因为一直无法run起来(没有error) : 但是无法将资料新增到database,以下为简化的code(排错0rz) : 在 app/models/mail_reader.rb中 : # app/models/mail_reader.rb : require 'net/pop' : : class MailReader < ActionMailer::Base : def receive(email) ^^^^^^^ 这是 MailReader 的 instance method : p "kerkerker" : #Q:这行都不会被执行0rz,不知道为什麽? : task = Task.new({:name => email.subject, :description => email.body}) : end [略] : mail = TMail::Mail.parse(email.pop) : p "Zeeee~~~" : MailReader.receive(mail) ^^^^^^^^^^^^^^^^^^ 这是 MailReader 的 class method (先这样理解,事实上 class method 也是一种 instance method.) : 目前就是确定有信的话他真的会印出Zeeee~~~,而且可以正确印出header等讯息(有先 : debug)但是没办法印出receive里面的kerkerker 0rz,不知道为什麽?想请教依下大 : 家 所以我这边才会说: : → godfat:instance method 和 class method 的差别 05/12 01 : → godfat:网页我现在读不出来..MailReader.receive(m)是class method 05/12 01 : → godfat:你定义 def receive 是 instance method, 本不会被呼叫 05/12 01 你在 MailReader.check_mail 里面用 MailReader.receive, 本来就不应该呼叫的 MailReader 的 instance method, 所以 科科 没出来,是正常的。但你现在好像没问题了, 那可能又是 rails 的 magic 吧,那我就不熟罗... 而根据这边: http://api.rubyonrails.org/classes/ActionMailer/Base.html Public Class methods: receive(raw_email) If you want your mailer to be able to process incoming messages, you'll need to implement a receive method that accepts the email object as a parameter. 意思就是 ActionMailer::Base 有个 class method 是 receive, 而这 method 底下又会呼叫 instance method receive, 看他 source code: 413: def receive(raw_email) 414: logger.info "Received mail:\n #{raw_email}" unless logger.nil? 415: mail = TMail::Mail.parse(raw_email) 416: mail.base64_decode 417: new.receive(mail) 418: end 最下面是 new.receive(mail), 因此他在这里面产生新的 ActionMailer::Base, 也就是你的 MailReader, 然後才呼叫你的 MailReader 的 instance method receive 如果你的 科科 没有被印出来,可能就是上面有 exception, 所以你的 receive 就被跳过去了。 == 补个结论,所以是不用加 self 没错,网页应该没写错(虽然我现在看不到) 只是要记得 MailReader.receive 并不会直接呼叫到你定义的 receive, 中间还有转过一手,如果你的 receive 没被叫到,要检查中间是谁转手的。 -- 「行け!Loki!」(rocky ロッキー) -Gurumin ぐるみん 王子? XD --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.135.28.18 ※ 编辑: godfat 来自: 220.135.28.18 (05/12 02:14)
1F:→ prelight:感谢大大无私解惑 谢谢你:) 05/12 12:13
2F:→ prelight:我还是老实的找本Ruby的书看一下相关的章节好了:) 05/12 12:22
3F:→ godfat:不谢,有其他问题欢迎来板上问 XD 05/12 14:11







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP