加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 通过localhost在ROR应用程序中向注册用户发送

发布时间:2020-12-16 20:52:10 所属栏目:百科 来源:网络整理
导读:在我的ROR应用程序中,我正在尝试在注册时向我的注册用户发送确认电子邮件,我的网站目前在localhost上.我收到此错误: "undefined method `recipients' for #UserMailer:0x3d841a0" 这是我的代码; development.rb config.action_mailer.delivery_method = :sm
在我的ROR应用程序中,我正在尝试在注册时向我的注册用户发送确认电子邮件,我的网站目前在localhost上.我收到此错误:
"undefined method `recipients' for #<UserMailer:0x3d841a0>"

这是我的代码;

development.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"
}

Users_controller.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

解决方法

** development.rb **
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

** Users_controller.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 %>

这就是我在开发模式中所做的,它的工作原理

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读