0

Currently trying to integrate a confirmation mail with devise authentication. I followed the instruction's from the devise documentation:

https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users

When i try to sign up a new user i get the following error:

NameError in Devise::RegistrationsController#create
undefined local variable or method `confirmed_at' for #<User:0x9b87b38>

User.rb

  class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable



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

rails g migration add_confirmable_to_devise

    class AddConfirmableToDevise < ActiveRecord::Migration
  # Note: You can't use change, as User.update_all will fail in the down migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    # add_column :users, :unconfirmed_email, :string # Only if using reconfirmable
    add_index :users, :confirmation_token, unique: true
    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    execute("UPDATE users SET confirmed_at = NOW()")
    # All existing user accounts should be able to log in after this.
    # Remind: Rails using SQLite as default. And SQLite has no such function :NOW.
    # Use :date('now') instead of :NOW when using SQLite.
    # => execute("UPDATE users SET confirmed_at = date('now')")
    # Or => User.all.update_all confirmed_at: Time.now
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
    # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
  end
end

confirmations_controller.rb

class ConfirmationsController < Devise::ConfirmationsController
  private
  def after_confirmation_path_for(resource_name, resource)
    your_new_after_confirmation_path
  end
end

routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers:{ confirmations: 'confirmations'}
  resources :videos
  get 'welcome/index'

  get 'welcome/new'

  root 'welcome#index'



end

I also saw someone saying i should add gem 'simple_token_authentication to my Gemfile and run

    rails g migration add_authentication_token_to_users authentication_token:string:index
rake db:migrate

However, that didn't fix that problem.

Any ideas? Thanks!

Prometheus
  • 799
  • 8
  • 28

1 Answers1

1

For each column in your database, ActiveRecord creates a method to both "set" and "get" the attribute (to the class, as a column represented in your database), named after the column on the class. In this case, the class is User, the column is confirmed_at.

What the error message is telling you is that there is no method on the User class. You can see methods by calling User.methods.

Without seeing your latest schema.rb file, I'm going to assume that you're missing the Devise confirmable migrations.

Create a new migration file, rails g migration AddConfirmableToUsers; head to db/migrations and open the corresponding migration file, and then copy-paste this in:

class AddConfirmableToUsers < ActiveRecord::Migration def change change_table(:users) do |t| # Confirmable t.string :confirmation_token t.datetime :confirmed_at t.datetime :confirmation_sent_at t.string :unconfirmed_email # Only if using reconfirmable end add_index :users, :confirmation_token, :unique => true end end

then invoke rake db:migrate.

Josh Brody
  • 5,153
  • 1
  • 14
  • 25
  • Thanks! That was the problem! Is there anything i need to do in order to make it finally send an email? Because right now i can finally sign up again but no confirmation email has been send. – Prometheus Jan 11 '17 at 21:58
  • There's not much, but the devil is in the details. I would check out mailcatcher.me to "send" mail in development (personal preference; you'll understand why I'm quoting "send" later), and then read something like this: https://richonrails.com/articles/debugging-emails-with-mailcatcher – Josh Brody Jan 11 '17 at 22:20
  • I'll check that out. Thanks for helping so much! – Prometheus Jan 12 '17 at 11:01