Reputation: 2845
I'm following Ryan Bates' tutorial on a Password Reset, but am having trouble completing it.
When I try to run the password reset, I get the error --
Missing template user_mailer/password_reset with "mailer". Searched in: * "user_mailer"
*I'm not sure why it's saying that, I have a password_reset.text.erb file under app/views/user_mailer/password_reset.text.erb*
Here's
Password_resets Controller
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 %>
User Model
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: "[email protected]"
# 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
App/views/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.
All help appreciated :)
Upvotes: 0
Views: 3707
Reputation: 911
password_reset.text.erb change its name to password_reset.html.erb
Upvotes: 2