2015-10-27 51 views
0

我正在使用自定義的設計會話控制器來管理我的用戶會話,但是無論何時我嘗試以現有用戶身份登錄,我的登錄功能都不起作用,它只是返回表單中的登錄,並且服務器返回此消息:用戶會話沒有被創建,任何想法爲什麼不?

Started POST "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2015-10-27 13:19:46 +0200 
ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations" 
Processing by Users::SessionsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lLtGottAklgEmCS2Y04FFZw3vAtd6EHkKOQMBCOJ4B6yeuFvN34j4OhYz9vd0SzW+gAwCI7GobMs20ubug24Fw==", "user"=>{"cell_number"=>"0798900606", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} 
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."cell_number" = $1 LIMIT 1 [["cell_number", "0798900606"]] 
Completed 401 Unauthorized in 45ms (ActiveRecord: 2.5ms) 
Processing by Users::SessionsController#new as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lLtGottAklgEmCS2Y04FFZw3vAtd6EHkKOQMBCOJ4B6yeuFvN34j4OhYz9vd0SzW+gAwCI7GobMs20ubug24Fw==", "user"=>{"cell_number"=>"0798900606", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} 
Rendered users/sessions/new.html.slim within layouts/application (60.1ms) 
Rendered application/_analytics.html.slim (4.7ms) 
Rendered application/_environment_indicator.html.slim (2.5ms) 
Rendered application/_preloader.html.slim (2.4ms) 
Rendered application/_flashes.html.slim (3.7ms) 
Category Load (0.7ms) SELECT "categories".* FROM "categories" WHERE "categories"."uuid" IS NULL LIMIT 1 
Completed 200 OK in 746ms (Views: 660.3ms | ActiveRecord: 0.7ms) 

我的會話控制器看起來是這樣的:

class Users::SessionsController < Devise::SessionsController 
before_action :configure_sign_in_parameters 

    def new 
    super 
    end 

    def create 
    @user = User.find_by(cell_number: params[:user][:cell_number]) 
    super 
    end 

    private 

    def after_sign_in_path_for(resource) 
    root_path 
    end 

    def configure_sign_in_parameters 
    devise_parameter_sanitizer.for(:sign_in).push(:cell_number, :password) 
    end 

end 

我的路線是這樣的:

Rails.application.routes.draw do 

### Admin 
devise_for :admin_users, ActiveAdmin::Devise.config 
ActiveAdmin.routes(self) 

### User 
devise_for :users, controllers: { :registrations => "users/registrations", 
           :sessions => "users/sessions", 
           :passwords => "users/passwords" } 

    devise_scope :user do 
    namespace :vodacom do 
    namespace :users do 
     get "/register", to: 'registrations#new' 
     get "/create", to: 'registrations#create' 
    end 
    end 
    end 
end 

和我的登錄窗體如下所示:

h2 Normal Log in 

= simple_form_for @user, url: user_session_path(@user) do |f| 
    div class="form-inputs" 
    = f.input :cell_number, required: true, autofocus: true 
    = f.input :password, required: true 
    = f.input :remember_me, as: :boolean if devise_mapping.rememberable? 

    div class="form-actions" 
    = f.button :submit, "Log in" 

任何線索我做錯了什麼?我的應用程序控制器指定用戶應在進行任何操作之前進行身份驗證。

+0

'完成401授權在45ms(ActiveRecord的:爲2.5ms)' - 登錄失敗。 –

回答

0

我弄清楚我做錯了什麼,我的用戶模型沒有電子郵件屬性,而是有一個cell_number屬性。所以,因爲我使用設計,設計默認的「電子郵件」作爲模型的身份驗證密鑰。爲了解決這個問題,我不得不指定cell_number在我的用戶模型的驗證密鑰:

devise :database_authenticatable, 
     :registerable, 
     :recoverable, 
     :rememberable, 
     :registerable, 
     :authentication_keys => [:cell_number] 
相關問題