且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Devise中自动保留用户的记忆

更新时间:2022-10-14 23:35:10

我认为定制你的设计控制器是去这里的方式。



目标:为每个人自动设置记住。



首先,创建一个设计会话控制器。让我们告诉铁路线路



config / routes.rb

  devise_for:users,:controllers => {:sessions => 'sessions'} 

app / controllers / sessions_controller.rb

  class SessionsController< Devise :: SessionsController 

def create
params [:user] .merge!(remember_me:1)
super
end

end

这样,用户记住我将永远设置为true。你可以编辑登录表单以显示remember_me复选框。



另外,将初始化器中的这个更改为远离的位置:

 #config.remember_for = 2.weeks 
config.remember_for = 1年


I am building an app in which I would like the users to automatically be remembered on their computers, without having a "remember me" check box.

I read that I may have to call @user.remember_me!, but not sure where to call it since the Devise controllers are hidden.

I was considering adding a hidden checkbox field in the sign_in form with the checkbox marked by default, but I was hoping I could do this on the controllers side.

Any idea how this could be done?

Thanks!

I think customizing your devise controller is the way to go here.

Goal: automatically set remember-me for everybody.

First, create a devise sessions controller. Let's tell rails routes about it

config/routes.rb

devise_for :users, :controllers => {:sessions => 'sessions'}

app/controllers/sessions_controller.rb

class SessionsController < Devise::SessionsController

  def create
    params[:user].merge!(remember_me: 1)
    super
  end

end

This way, the user's remember me will always be set to true. yay!

You'll then want to edit the login form to not display the remember_me checkbox.

Also, change this in the initializer to something far off:

#config.remember_for = 2.weeks
config.remember_for = 1.year