1

I am using Rails 4 and devise 3.2. At sign up time, an activation/confirmation link is sent by e-mail to the newly registered user. I have implemented this from this article in the Devise wiki.

Whenever I put same passwords they login without any problem, but it's not working when I make a error of password doesn't match in 1st attempt and put same password in 2nd attempt.

This is the error which shows up

NoMethodError in ConfirmationsController#update
undefined method `add_error_on' for ConfirmationsController:Class

confirmation controller

class ConfirmationsController < Devise::ConfirmationsController

  skip_before_filter :require_no_authentication
  skip_before_filter :authenticate_user!

  # PUT /resource/confirmation
  def update
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        @confirmable.attempt_set_password(params[:user])
        if @confirmable.valid? and @confirmable.password_match?
          do_confirm
        else
          do_show
          @confirmable.errors.clear #so that we wont render :new
        end
      else
        self.class.add_error_on(self, :email, :password_already_set)
      end
    end

    if !@confirmable.errors.empty?
      render 'devise/confirmations/new'
    end
  end

  # GET /resource/confirmation?confirmation_token=abcdef
  def show
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        do_show
      else
        do_confirm
      end
    end
    if !@confirmable.errors.empty?
      self.resource = @confirmable
      render 'devise/confirmations/new'
    end
  end

  protected

  def with_unconfirmed_confirmable
    original_token = params[:confirmation_token]
    confirmation_token = Devise.token_generator.digest(User, :confirmation_token, original_token)
    @confirmable = User.find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
    if !@confirmable.new_record?
  
      @confirmable.only_if_unconfirmed {yield}
    end
  end

  def do_show
    @confirmation_token = params[:confirmation_token]
    @requires_password = true
    self.resource = @confirmable
    render 'devise/confirmations/show'
  end

  def do_confirm
    @confirmable.confirm!
    set_flash_message :notice, :confirmed
    sign_in_and_redirect(resource_name, @confirmable)
   end
 end

Please help. Thank you.

Community
  • 1
  • 1
dips
  • 370
  • 6
  • 17

1 Answers1

5

The error case for the update action in that wiki article is out of date. There is no longer an add_error_on method in Devise. Instead, you can just use the basic ActiveRecord errors object:

@confirmable.errors.add(:email, :password_already_set)

Then, you'll still have a problem, because it needs to assign @confirmable to resource in order for the default views to work correctly (the wiki example does this for all the other error renders, just not this one):

if !@confirmable.errors.empty?
  self.resource = @confirmable
  render 'devise/confirmations/new'
end
Louis Simoneau
  • 1,781
  • 14
  • 18
  • Still I'm getting the error- "Email translation missing: en.activerecord.errors.models.user.attributes.email.password_already_set". It require us to resend the confirmation link. When that link is clicked, user is logged in even if the password entered second time didn't match. – dips Aug 28 '14 at 11:45
  • This fixed the issue but now this pages goes to "Resend confirmation instructions " page.. any idea, why it is going on Resend Confirmation page ? – kashif Aug 28 '14 at 14:18
  • 1
    The translation error is because Devise doesn't include an error message for "password_already_set" by default. You can add it to devise.en.yml (or your locale file). Regarding the non-matching password, are you using the validatable module? – Louis Simoneau Aug 29 '14 at 01:36
  • I removed this condition-if @confirmable.has_no_password?.. everything works fine. ThankYou. Can you tell me when is this condition(if @confirmable.has_no_password?) helpful or applicable in the sign Up process? – dips Sep 09 '14 at 09:14
  • @LouisSimoneau thanks for the info. Looks like the Rails 4 doc is out of date for that whole thing and could use some attention. – tirdadc Mar 31 '15 at 17:44