在使用user_signed_in进行操作之前?签入应用程序控制器进行数据库调用并返回用户

时间:2019-02-13 14:14:31

标签: ruby-on-rails devise

尝试按照https://github.com/plataformatec/devise/#configuring-controllers自定义登录行为。

使用Rails 5.2.2,ruby 2.5.3,设计4.6.1。

我用过user_signed_in吗?在application_controller.rb中使用before_action进行选择查询并生成current_user对象。

这是否意味着用户已登录?这是预期的行为吗?

如果是的话,如何在application_controller中检查用户登录,我需要根据用户详细信息设置应用程序级别的值。

如果我做错了,请帮助我并纠正我。

未对默认的 devise.rb

进行任何更改

routes.rb

Rails.application.routes.draw do
  resources :profiles
  devise_for :users, controllers: { sessions: 'users/sessions' }
  root to: "profiles#index"
end

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery prepend: true
  before_action :simple_call, if: proc { user_signed_in? }
  ## USER BELOW LINE AS WELL BY COMMENTING ABOVE RESULTS SAME
  #before_action :simple_call, if: proc { current_user.present? }
  def  simple_call
    p '--------CURRENT_USER----'
    p current_user
  end
end

users / sessions_controller.rb

# frozen_string_literal: true

class Users::SessionsController < Devise::SessionsController
  # before_action :configure_sign_in_params, only: [:create]

  # GET /resource/sign_in
  # def new
  #   super
  # end

  # POST /resource/sign_in
  def create
    p 'this line is printing after before action'
    super do |resource|
     p 'this is inside create'
    end
  end

  # DELETE /resource/sign_out
  # def destroy
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
  # end
end

在登录期间

登录

Started GET "/" for 172.xx.xx.xx at 2019-02-13 19:25:57 +0530
Cannot render console from 172.xx.xx.xx! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::SessionsController#create as HTML
  Parameters: {"utf8"=>"â", "authenticity_token"=>"Y01fmO/qtAbNO7QcS6lXc                                                         bOj0vSWXEnlYGlnrNT7TDOm4/yoJRWfSdknzpUMK2mKeMGrQaCEJa07Tn6I10Z2QQ==", "u                                                         ser"=>{"email"=>"k@t.com", "password"=>"[FILTERED]", "remember_me"=>"0                                                         "}, "commit"=>"Log in"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."email                                                         " = ? ORDER BY "users"."id" ASC LIMIT ?  [["email", "k@t.com"], ["LIMI                                                         T", 1]]
  â³ app/controllers/application_controller.rb:3
"--------CURRENT_USER----"
#<User id: 1, email: "k@t.com", created_at: "2019-02-13 13:16:34", upd                                                         ated_at: "2019-02-13 13:16:34">
"this line is printing after before action"
"this is inside create"
Redirected to http://172.xx.xx.xx:3004/
Completed 302 Found in 141ms (ActiveRecord: 0.4ms)

更新

请参阅此讨论。 https://github.com/plataformatec/devise/issues/4951

我相信在即将发布的版本中将解决此问题。

1 个答案:

答案 0 :(得分:1)

您很有可能需要先致电before_filter :authenticate_user!,然后user_signed_in?支票才能正常运行。