gitastic
gitastic

Reputation: 526

rails, devise recoverable not sending emails

i have devise set up in my rails app. i added confirmable, and have emails sent to confirm email accounts and it is sending properly in development env.

i added the recoverable option as well:

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

but when i go to the "forgot my password", and enter an email, it does not send anything. I'm really struggling trying to figure out why the recoverable email is not working. I'm pretty sure it's not my email settings because it works with confirmable. Can somebody provide any thoughts on debugging? or where to start looking into why recoverable is not working?

Thanks so much in advance.

my log when i press the submit button on forgot my password:

Started POST "/users/password" for 127.0.0.1 at 2014-11-28 01:54:07 -0500
  ActiveRecord::SchemaMigration Load (1.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PasswordsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"bGrcwyIqwkeC51TOLyoDqX1bTj2XKNH9RHU7qJ01Zfs=", "user"=>{"email"=>""}, "commit"=>"Reset Password"}
  Rendered devise/passwords/new.html.erb within layouts/application (8.8ms)
  Rendered layouts/_header.html.erb (4.2ms)

here is the log when i sign up a new user (confirmable):

 Rendered devise/mailer/confirmation_instructions.html.erb (1.4ms)

Devise::Mailer#confirmation_instructions: processed outbound mail in 33.6ms

Sent mail to [email protected] (223.6ms)
Date: Fri, 28 Nov 2014 02:29:45 -0500
From: test
Reply-To: test
To: [email protected]
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

why doesn't recoverable activate the devise::mailer?

my User.rb:

class User < ActiveRecord::Base

  extend FriendlyId
  friendly_id :username, use: [:slugged, :finders]

  def should_generate_new_friendly_id?
    username_changed?
  end

  #validates :username, :uniqueness => {:case_sensitive => false},
  #:format => { with: /\A[a-zA-Z0-9]+\Z/ }

  validates :firstname, presence: true
  validates :lastname, presence: true
  validates :country, presence: true
  validates :birthday, presence: true

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  attr_accessor :login

  validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 1.megabytes

  acts_as_voter
  acts_as_messageable
  acts_as_votable
  is_impressionable

  has_attached_file :avatar, :styles => { 
      :medium => "300x300>", 
      :thumb => "100x100#", 
      :small => "50x50#",
      :tiny => "35x35>",
      :header => "30x30#" }, 
      default_url: ActionController::Base.helpers.image_path('silhouette.png')

  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

   def self.find_first_by_auth_conditions(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      else
        where(conditions).first
      end
    end
end

Upvotes: 1

Views: 1747

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Have you added proper fields required for recoverable module in the users table? If not, then run this migration

class AddRecoverableToUsers < ActiveRecord::Migration
  def change
    add_column :users, :reset_password_token, :string
    add_column :users, :reset_password_sent_at, :datetime
  end
end

After running this, restart the server and try again

Or, inside form_for tag in devise/password/new.html.erb add these lines, you will get the exact errors on your view

<% if f.object.errors.any? %>
  <ul>
    <% f.object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

So, as your username field is creating problem, you can add a before_update callback, try this

before_validation :check_for_reset_token, if: Proc.new{ |user| user.reset_password_token_changed? }

def check_for_reset_token
  self.username = username
end

Upvotes: 1

fresh5447
fresh5447

Reputation: 1340

Just to make sure, can you confirm these steps:

Confirm that you have this in your config/environments/developement.rb

   config.action_mailer.default_url_options = { host: 'localhost:3000' }
   config.action_mailer.delivery_method = :smtp
   config.action_mailer.perform_deliveries = true

Can you double check that you uncommented all these lines when you ran the migration:

   t.string   :confirmation_token
   t.datetime :confirmed_at
   t.datetime :confirmation_sent_at
   t.string   :unconfirmed_email # Only if using reconfirmable

I would also reccommend this in config/environments/developement.rb

config.action_mailer.raise_delivery_errors = true

Upvotes: 1

Related Questions