缺少模板rails mailer开发环境

时间:2013-12-23 10:47:09

标签: ruby-on-rails actionmailer

我正在关注密码重置的Ryan Bates教程,但我在完成它时遇到了麻烦。

当我尝试运行密码重置时,我收到错误 -

使用“邮件程序”缺少模板user_mailer / password_reset。搜索:*“user_mailer”

*我不确定为什么会这样说,我在app / views / user_mailer / password_reset.text.erb *下有一个password_reset.text.erb文件

这是

Password_resets控制器

class PasswordResetsController < ApplicationController
  def new
  end

  def create
    user = User.find_by_email(params[:email])
    user.send_password_reset if user
    redirect_to root_url, :notice => "Email sent with password reset instructions"
  end
end

Password_resets#new.html.erb

<% provide(:title, "Reset Password") %>

<%= form_tag password_resets_path, :method => :post do %>
  <div class="field">
    <%= label_tag :email %>
    <%= text_field_tag :email, params[:email] %>
  </div>
  <div class="actions"><%= submit_tag "Reset Password", :class => "button" %></div>
<% end %>

用户模型

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  before_create :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, 
            format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }, on: :create
  #ask at one point, wherther I can continue to validate password and password_confirmation, on update, but allow password_reset to work
  has_one :common_app, dependent: :destroy


  has_one :video, dependent: :destroy

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column) # this is similar to create_remember_token, but instead it's generalized, so it can work on any column
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end


  private

    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    end

end

User_mailer.rb

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

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_rest.subject
  #
def password_reset(user)
  @user = user
  mail :to => user.email, :subject => "Password Reset"
end

end

应用/视图/ user_mailer文件/ password_reset.text.erb

Click the link below to reset your password.

<%= edit_password_reset_url(@user.password_reset_token) %>

If you did not request to change your password, ignore this email and your password will stay the same.

所有帮助表示赞赏:)

1 个答案:

答案 0 :(得分:2)

password_reset.text.erb将其名称更改为password_reset.html.erb