2
votes
  1. User signed up
  2. User received confirmation email
  3. User click on user_confirmation_url(@resource, :confirmation_token => @token)
  4. User goes to /users/confirmation.39?confirmation_token=V-UwSF5qzCt8mBVAFuwK
  5. It get this error

enter image description here

If i manually change the url to: users/confirmation/39?confirmation_token=V-UwSF5qzCt8mBVAFuwK

I get this error: The action 'confirmation' could not be found for UsersController

Routes.rb

      App::Application.routes.draw do

  get "pages/quickstart"
  get "pages/configuration"
  get "pages/invoices"
  get "/reports" => "reports#index" 
  get "/reports/historical_data" => "reports#historical_data", as: :historical_data

  #get "statements/document/month/:date"    => "statements#month", :document => true, as: :monthly_statements_document
  #get "statements/month/:date/" => "statements#month", as: :monthly_statements

  resources :reminders
  resources :reminder_users
  resources :forecasts
  resources :statements
  resources :monthly_statements
  resources :fuel_cards
  resources :accounts_payables
  resources :accounts_receivables
  resources :customers
  resources :invoices
  resources :suppliers
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)

  devise_for :users, :controllers => { :registrations => "registrations"}

  # You can have the root of your site routed with "root"
  root 'pages#quickstart', as: :home

  get "/home" => "home#welcome"
  get "/registrations", to: redirect('/users/edit')
  get "/user", to: redirect('/users/edit')
  get "/user/change_password", as: :change_password
  match ':controller(/:action(/:id))(.:format)', via: [:get, :post]

end

(I use this to allow user to edit their profile)

              user_confirmation POST       /users/confirmation(.:format)                      devise/confirmations#create
          new_user_confirmation GET        /users/confirmation/new(.:format)                  devise/confirmations#new
                                GET        /users/confirmation(.:format)                      devise/confirmations#show

Model

class User < ActiveRecord::Base

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

UserController

class UsersController < ApplicationController

  before_filter :authenticate_user!

  def edit_password
    @user = current_user
  end

  def update_password
    @user = User.find(current_user.id)
    if @user.update(user_params)
      # Sign in the user by passing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end

  private

  def user_params
    # NOTE: Using `strong_parameters` gem
    params.required(:user).permit(:password, :password_confirmation)
  end
end

RegistrationController

class RegistrationsController < Devise::RegistrationsController
  def update
    @user = User.find(current_user.id)

    successfully_updated = if needs_password?(@user, params)
      @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
    else
      # remove the virtual current_password attribute
      # update_without_password doesn't know how to ignore it
      params[:user].delete(:current_password)
      @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
    end

    if successfully_updated
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      #redirect_to after_update_path_for(@user)
      redirect_to edit_user_registration_path
    else
      render "edit"
    end
  end

  private

  # check if we need password to update user data
  # ie if password or email was changed
  # extend this as needed
  def needs_password?(user, params)
    user.email != params[:user][:email] ||
      params[:user][:password].present?
  end
end

devise (3.4.1)

1
Could you post the output from rake routes | grep confirmation please? - Prakash Murthy
ok, done. Question updated - sparkle
It is likely that you defined resources :users or something similar before the devise_for call in your router, and that is confusing Rails route generation. If you post the full router, it will help us identifying it. - José Valim
I'm not sure what version of devise you are using but shouldn't it be just user_confirmation_url(:confirmation_token => @token) (without @resource)? - katafrakt
Have you found a solution yet? I'm facing the same problem. - hello_its_me

1 Answers

0
votes

The issue here is with the view generators.

Do you see, in your path, you are getting users/confirmation.39 for your confirmation route...that 39 is being interpreted as a format by your controller. Its not expecting a user id (@resource) if you have a look at rake routes

the answer here is to remove @resource from your url_helper in the email/view, and if needed, pass ID in as a parameter with the token

user_confirmation_url(confirmation_token: @token, id: @resource)