ruby-on-rails-4devisedevise-confirmabledevise-token-auth

why devise is generating this format of confirmation URL?


devise keep on generating this format of confirmation URL

http://something.com/users/confirmation/divyanshu-rawat?confirmation_token=CV3zV1wAWsb3RokHHEKN

I don't know why it is not generating something like this.

http://something.com/users/confirmation?confirmation_token=CV3zV1wAWsb3RokHHEKN

This is how my confirmation_instructions.html.haml looks like.

%p Welcome #{@resource.first_name}!
%p You can confirm your account email through the link below:
%p= link_to 'Confirm my account', user_confirmation_url(@resource, :confirmation_token => @resource.confirmation_token)

Solution

  • In Devise gem, routes for confirmation are created as below,

    #  # Confirmation routes for Confirmable, if User model has :confirmable configured
    #  new_user_confirmation GET    /users/confirmation/new(.:format) {controller:"devise/confirmations", action:"new"}
    #      user_confirmation GET    /users/confirmation(.:format)     {controller:"devise/confirmations", action:"show"}
    #                        POST   /users/confirmation(.:format)     {controller:"devise/confirmations", action:"create"}
    

    So if you want to create url like,

    http://something.com/users/confirmation?confirmation_token=CV3zV1wAWsb3RokHHEKN
    

    Use

    user_confirmation_url(confirmation_token: @resource.confirmation_token)`
    

    Instead of

    user_confirmation_url(@resource, confirmation_token: @resource.confirmation_token)`
    

    Also check routes.rb

    If you want to pass user_name or name db attribute of @resource in confirmation url (as you asked by passing 'divyanshu-rawat' in your url), You can create own custom route which will point to same controller & action as below,

      # config/routes.rb
      devise_for :users
    
      as :user do
        get  '/users/confirmation/:name' => "devise/confirmations#show", as: 'user_confirm'
      end 
    

    And if in your case, @resource.user_name = 'divyanshu-rawat', update confirmation_instructions.html.haml as below,

    %p Welcome #{@resource.first_name}!
    %p You can confirm your account email through the link below:
    %p= link_to 'Confirm my account', user_confirm_url(name: @resource.user_name, confirmation_token: @resource.confirmation_token)
    

    Which will produce url like,

    http://something.com/users/confirmation/divyanshu-rawat?confirmation_token=CV3zV1wAWsb3RokHHEKN