ActionMailer Rails Method Errors

I implemented an authentication system using authlogic and added the reset password functionality according to this tutorial http://github.com/rejeep/authlogic-password-reset-tutorial

Everything works, but I'm confused why this is happening.

There is this code.

class User < ActiveRecord::Base
  def deliver_password_reset_instructions!
    reset_perishable_token!
    Notifier.deliver_password_reset_instructions(self)
  end
end

and this one

class Notifier < ActionMailer::Base
  def password_reset_instructions(user)
    subject      "Password Reset Instructions"
    from         "noreplay@domain.com"
    recipients   user.email
    content_type "text/html"
    sent_on      Time.now
    body         :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
  end
end

This line bothers me

Notifier.deliver_password_reset_instructions(self)

since the method of the Notifier class is called

password_reset_instructions

without a bit deliver_.

So what's going on here? how does it work

+3
source share
2 answers

Great question. Many people forget to ask these questions.

, . mailer deliver_<method_name>

, Ruby method_missing ruby. , , , ruby ​​ method_missing. "ActionMailer:: Base"

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

, "deliver_" , Rails Mailer ( "" ) "!". , .

"create_ <method_name > "

+3

Rails 2 ActionMailer.

, YourMailerClass.deliver_NAME_OF_THE_METHOD. , YourMailerClass.create_NAME_OF_THE_METHOD.

Rails Mailer, .

+1

Source: https://habr.com/ru/post/1769972/


All Articles