In Devise, I'm signing in my user like this:
sign_in_and_redirect(:user, user)
In the default sign in page, there's a checkbox 开发者_JAVA百科that the user can select so that they don't have to sign in again when they return to the site. But when you do the sign in with the sign_in_and_redirect(:user, user)
line, I can't work out how to set that parameter to yes. Does anyone know how? Thanks for reading.
current_user.remember_me!
https://github.com/plataformatec/devise/blob/master/lib/devise/models/rememberable.rb#L54
Note that this only updates the remember_created_at
value of the user record. But for this to work correctly, a validation token also needs to be stored in the Devise cookie. To achieve both these things, follow Dmytrii's advise and user the remember_me <USER>
controller method instead:
include Devise::Controllers::Rememberable
...
remember_me <USER_OBJECT>
Did some testing. Presenting the findings for others.
The simplest solution, assuming the user object has the rememberable module defined on the devise
declarable, is to set remember_me to true on the user before sign in and redirect:
@user.remember_me = true
sign_in_and_redirect(@user, :event => :authentication)
精彩评论