0

Derzeit versuchen, eine Bestätigungsmail mit Devise-Authentifizierung zu integrieren. Ich folgte den Anweisungen des von der devise Dokumentation:Rails Devices bestätigbarer Fehler

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

Wenn ich versuche, einen neuen Benutzer ich folgende Fehlermeldung erhalten, um sich zu registrieren:

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 

Schienen 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 

Ich sah auch jemand sagen, ich gem ‚simple_token_authentication meiner Gemfile hinzufügen sollten und führen

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

Allerdings hat das das Problem nicht behoben.

Irgendwelche Ideen? Vielen Dank!

+0

Überprüfen Sie die Datei 'db/schema.rb', um sicherzustellen, dass die Tabelle' users' die Spalte 'confirmed_at' enthält. – chumakoff

Antwort

0

ActiveRecord erstellt für jede Spalte in Ihrer Datenbank eine Methode zum "Setzen" und "Abrufen" des Attributs (für die Klasse als Spalte in Ihrer Datenbank), benannt nach der Spalte der Klasse. In diesem Fall lautet die Klasse "Benutzer", die Spalte lautet "confirmed_at".

Was die Fehlermeldung Ihnen sagt, ist, dass es keine Methode für die Benutzerklasse gibt. Sie können Methoden anzeigen, indem Sie User.methods aufrufen.

Ohne Ihre neueste schema.rb-Datei zu sehen, gehe ich davon aus, dass Ihnen die bestätigbaren Migrationen von Devise fehlen.

Erstellen Sie eine neue Migrationsdatei, rails g migration AddConfirmableToUsers; Kopf zu db/Migrationen und öffnet die entsprechende Migrationsdatei, und dann dieses in kopieren und einfügen:

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

dann ruft rake db:migrate

.

+0

Danke! Das war das Problem! Gibt es etwas, was ich tun muss, damit es endlich eine E-Mail sendet? Weil ich mich jetzt endlich wieder anmelden kann, aber noch keine Bestätigungsemail gesendet wurde. – Prometheus

+0

Es gibt nicht viel, aber der Teufel steckt im Detail. Ich würde mailcatcher.me auschecken, um Mail in der Entwicklung zu "senden" (persönliche Präferenz; Sie werden verstehen, warum ich später "send" zitiere), und dann etwas wie das lesen: https://richonrails.com/articles/ debugging-email-with-mailcatcher –

+0

Ich werde das überprüfen. Danke, dass du so viel geholfen hast! – Prometheus