Ruby on rails Rails ActionMailer方法混乱
我使用authlogic实现了一个身份验证系统,并根据本教程添加了密码重置功能 这一切都起作用了,但我不明白为什么会起作用 这是一个密码Ruby on rails Rails ActionMailer方法混乱,ruby-on-rails,authlogic,actionmailer,Ruby On Rails,Authlogic,Actionmailer,我使用authlogic实现了一个身份验证系统,并根据本教程添加了密码重置功能 这一切都起作用了,但我不明白为什么会起作用 这是一个密码 class User < ActiveRecord::Base def deliver_password_reset_instructions! reset_perishable_token! Notifier.deliver_password_reset_instructions(self) end end 当调用通知程序类中
class User < ActiveRecord::Base
def deliver_password_reset_instructions!
reset_perishable_token!
Notifier.deliver_password_reset_instructions(self)
end
end
当调用通知程序类中的方法时
password_reset_instructions
没有交付
位
这是怎么回事?这是Rails 2 ActionMailer的惯例 要发送电子邮件,请使用您的邮件类。发送方法的名称。 要初始化电子邮件,请使用
YourMailerClass。创建\u方法的\u名称\u
Rails将自动创建Mailer类的实例,调用您的方法并传递电子邮件对象。好问题,很多人忘记问这些问题 您是否注意到,您从未真正实例化过mailer类对象。您的mailer类方法通常用这个
deliver\uu
因此在ruby内部使用了method\u missing
的概念。假设您在ruby中调用某个对象不存在的方法,ruby将调用方法\u missing
方法。这是在“ActionMailer::Base”代码中定义的
def method_缺失(method_符号,*参数)#:nodoc:
case方法_symbol.id2name
当/^create([\u a-z]\w*)/然后新建($1,*参数)。邮件
当/^deliver([\u a-z]\w*)/然后是new($1,*参数)。deliver!
当“新”时,则为零
其他超级
结束
结束
因此,如果一个方法匹配“deliver”和小写字母的任意组合,Rails将实例化您的Mailer类(通过调用“new”)并将其与参数一起发送给初始值设定项,然后调用“deliver!”方法以最终传递邮件
“create_uu”类型的方法也是如此非常感谢您的详细解释,现在就有意义了。
Notifier.deliver_password_reset_instructions(self)
password_reset_instructions
def method_missing(method_symbol, *parameters)#:nodoc:
case method_symbol.id2name
when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
when "new" then nil
else super
end
end