我正在使用ActiveAdmin
自定义User
模型(不包含Devise
)和自定义SessionsController
。我成功安装了ActiveAdmin
,实施了必要的方法,但面临一个问题:在第一次访问仪表板后,用户remember_token
从Cookie中删除(我在浏览器中找到它)导致注销用户并重定向到网站的主页。
在Rails日志中,对用户模型进行了以下更改:
D, [2015-03-06T18:08:38.412548 #29316] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY login ASC LIMIT 1 [["remember_token", "d983c4abdc252ffc04a71260513ace78534a4c2b"]]
D, [2015-03-06T18:08:38.538903 #29316] DEBUG -- : (0.1ms) BEGIN
D, [2015-03-06T18:08:38.539994 #29316] DEBUG -- : SQL (0.2ms) UPDATE "users" SET "remember_token" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["remember_token", "5c40d43f2df2a0dfcbd0bc3a40496bf3eb5bf8a7"], ["updated_at", "2015-03-06 14:08:38.539165"], ["id", 22]]
D, [2015-03-06T18:08:38.593651 #29316] DEBUG -- : (53.4ms) COMMIT
D, [2015-03-06T18:08:38.594675 #29316] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY login ASC LIMIT 1 [["remember_token", "da39a3ee5e6b4b0d3255bfef95601890afd80709"]]
配置/初始化/ active_admin.rb
ActiveAdmin.setup do |config|
config.site_title = "Coordinate"
config.authentication_method = :authenticate_admin_user!
config.logout_link_path = :sign_out
config.logout_link_method = :delete
config.batch_actions = true
end
应用程序/控制器/ application_controller.rb
def authenticate_admin_user!
unless current_admin_user
flash[:error] = "Access error!"
redirect_to root_path
end
end
def current_admin_user
return nil if signed_in? && !current_user.admin?
current_user
end
应用程序/助手/ sessions_helper.rb
def sign_in(user)
remember_token = User.new_remember_token();
cookies.permanent[:remember_token] = remember_token;
user.update_attribute(:remember_token, User.encrypt(remember_token));
self.current_user=user;
end
def current_user
remember_token= User.encrypt(cookies[:remember_token]);
@current_user ||= User.find_by(remember_token: remember_token);
end
def current_user?(user)
user==current_user;
end
def signed_in?
!current_user.nil?
end
def sign_out()
current_user.update_attribute(:remember_token, User.encrypt(User.new_remember_token));
cookies.delete(:remember_token);
self.current_user=nil;
end
请帮助解决这个问题。
答案 0 :(得分:1)
current_user是零。对?
好像你没有根据你的remember_token
来自cookie初始化它。您应该使用authenticate_admin_user!
方法初始化它。
答案 1 :(得分:0)
我决定在配置文件ActiveAdmin中设置值config.logout_link_path = false