简体   繁体   English

Ruby on Rails Devise Gem,从控制器指定电子邮件模板

[英]Ruby on Rails Devise Gem, specify email template from a controller

I have a controller which inherited from ApplicationController where I send reset password insturctions, is there any way to specify template? 我有一个从ApplicationController继承的控制器,在该控制器中我发送重置密码指令,是否可以指定模板?

@user=User.find_by_email(email);
if @user.nil?
  render :status=>404, :json=>{:message=>"User not found or email format is invalid."}
  return
else
  @user.devise_mailer.reset_password_instructions(@user).deliver
  render :status=>200, :json=>{:message=>"Reset password instructions have been sent."}
end

As you see in the snippet above, @user.devise_mailer.reset_password_instructions(@user).deliver Sends reset password instructions, but it takes default email template, which I don't wanna change. 如您在上面的代码段中所见,@ user.devise_mailer.reset_password_instructions(@user).deliver发送重置密码说明,但是它使用默认的电子邮件模板,我不想更改。 I need to create another template and somehow specify to use it 我需要创建另一个模板并以某种方式指定使用它

Create your own mailer then. 然后创建您自己的邮件程序。 See http://guides.rubyonrails.org/action_mailer_basics.html for details. 有关详细信息,请参见http://guides.rubyonrails.org/action_mailer_basics.html Once you have a new mailer, in the controller, instead of: 一旦有了新的邮件程序,就在控制器中,而不是:

@user.devise_mailer.reset_password_instructions(@user).deliver

you'd do something like: 你会做类似的事情:

YourMailer.reset_password_instructions(@user).deliver

You may want to reuse parts of Devise in your mailer: check out devise sources for that: https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb 您可能想在邮件程序中重用部分Devise:请查看以下devise来源: https : //github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb

Another thing you may try is redefining devise template by putting a file named app/views/devise/mailer/reset_password_instructions.html.erb in your application directory. 您可以尝试的另一件事是通过在应用程序目录中放置一个名为app/views/devise/mailer/reset_password_instructions.html.erb的文件来重新定义app/views/devise/mailer/reset_password_instructions.html.erb Of course you can base your implementation on devise sources as well: https://github.com/plataformatec/devise/blob/master/app/views/devise/mailer/reset_password_instructions.html.erb 当然,您也可以基于devise的实现: https : //github.com/plataformatec/devise/blob/master/app/views/devise/mailer/reset_password_instructions.html.erb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM