1

在我的 ROR 应用程序中,我试图在注册用户注册时向他们发送确认电子邮件,我的网站目前位于本地主机上。我收到此错误:

"undefined method `recipients' for #<UserMailer:0x3d841a0>"

这是我的代码;

发展.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "localhost:3000"}
config.action_mailer.smtp_settings = {
      :address      => "smtp.gmail.com",
      :port          => "587",
      :authentication => :login,
      :user_name      => "myemailid@gmail.com",
      :password       => "myrealpassword"
}  

用户控制器.rb

def new
  UserMailer.registration_confirmation(@user).deliver
end

def create
  @user = User.new(params[:user])
  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    sign_in @user
    flash[:success] = "Welcome!"
    redirect_to @user
  else
    render 'new'
  end
end

user_mailer.rb

class UserMailer < ActionMailer::Base   
  def registration_confirmation(user)
    recipients   user.email
    from         "myemailid@gmail.com"
    subject      "Thank you for registration"
    body         :user => user  
  end
end
4

3 回答 3

1

你用的是什么版本的 Rails?

3.2 版更改了电子邮件的发送(我相信)

http://api.rubyonrails.org/classes/ActionMailer/Base.html

尝试:

UserMailer.registration_confirmation(@user).deliver

于 2012-06-12T07:14:42.417 回答
1

这是你的 user_mailer.rb

user_mailer.rb

class UserMailer < ActionMailer::Base
  def registration_confirmation(user)
    recipients   user.email
    from         "myemailid@gmail.com"
    subject      "Thank you for registration"
    body         :user => user  
  end

请尝试:

class UserMailer < ActionMailer::Base
  default from: "myemailid@gmail.com"

  def registration_confirmation(user)
    mail(to: user.email, subject: "Thank you for registration")
  end
end

并在views/user_mailer 中设置适当的视图,例如registration_confirmation.html.erb

于 2012-06-12T09:50:07.387 回答
1

**发展.rb**

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

** 用户控制器.rb **

def new
  UserMailer.registration_confirmation(@user).deliver
end
def create
  @user = User.new(params[:user])
  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    sign_in @user
    flash[:success] = "Welcome!"
    redirect_to @user
  else
    render 'new'
  end
end

** User_mailer.rb **

 def registration_confirmation(user) 
   @message = 'whatever you want to say here!'
   mail(:from => "myemailid@gmail.com", :to => user.email, :subject => "Thank you for registration")
end

**/app/views/user_mailer/registration_confirmation.text.erb *

<%= @message %>

这就是我在开发模式下所做的,并且有效

于 2012-06-12T10:03:43.883 回答