Ruby-on-rails Rails - 如何在用户登录时覆盖设计 SessionsController 以执行特定任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13836139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails - How to override devise SessionsController to perform specific tasks when user signs in?
提问by Patrice Navarre
Using Devise to manage users sessions / registrations I would need to perform specific tasks (updating some fields in the users table for this specific user for example) each time a user signs in, and before he gets redirected by devise to the home page for connected users.
使用 Devise 管理用户会话/注册我需要在每次用户登录时以及在他被设计重定向到主页以进行连接之前执行特定任务(例如更新该特定用户的用户表中的某些字段)用户。
Do I have to override devise SessionsController, and if yes, how?
我是否必须覆盖设计 SessionsController,如果是,如何?
回答by Pykih
Alternatively, you can create your own sessions controller
或者,您可以创建自己的会话控制器
class SessionsController < Devise::SessionsController
def new
super
end
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
if !session[:return_to].blank?
redirect_to session[:return_to]
session[:return_to] = nil
else
respond_with resource, :location => after_sign_in_path_for(resource)
end
end
end
And in routes.rbadd:
并routes.rb补充说:
devise_for :users, controllers: {sessions: "sessions"}
回答by Soundar Rathinasamy
Devise provides after_database_authenticationcallback method.You have full access for the current authenticated user object over there.
设计提供after_database_authentication回调方法。您可以完全访问那里的当前已验证用户对象。
If you want to update current user name after every successful login you can do that like below.
如果您想在每次成功登录后更新当前用户名,您可以像下面这样做。
class User < ActiveRecord::Base
devise :database_authenticatable
def after_database_authentication
self.update_attributes(:name => "your name goes here")
end
end
回答by stephen.hanson
If you look at Devise's implementationof sessions_controller#create, you'll notice that they yield if you pass a block.
如果你看一下设计的实现中sessions_controller#create,你会发现,如果你传递一个块他们屈服。
So, just subclass their sessions controllers and pass a block when you call super. To do that, first tell Devise in routes.rbthat you'd like to use your own sessions controller:
因此,只需将其会话控制器子类化并在调用 super 时传递一个块。为此,首先告诉 Deviseroutes.rb您想使用自己的会话控制器:
devise_for :users, controllers: { sessions: 'users/sessions' }
And then create a SessionsControllerclass and pass a block when you call super in your create method. It would look something like this:
然后SessionsController在创建方法中调用 super 时创建一个类并传递一个块。它看起来像这样:
class Users::SessionsController < Devise::SessionsController
layout "application"
# POST /login
def create
super do |user|
if user.persisted?
user.update(foo: :bar)
end
end
end
end
Most of the Devise controller methods accept a block, so you could do this for registration, forgot password, etc as well.
大多数设计控制器方法都接受一个块,因此您也可以在注册、忘记密码等时执行此操作。

