我想这样做:
application_controller.rb:
class ApplicationController < ActionController::Base
alias_method :devise_current_user, :current_user
private
def current_user
if params[:user].blank?
puts "!found user"
devise_current_user
else
puts "found user"
user = User.find_by(email: params[:user][:email])
return detect_role(user)
end
end
def detect_role(user)
roles = user.roles_name
user = if roles.include?("mentor")
user.becomes(Mentor)
elsif !roles.include?("admin") && !roles.include?("mentor")
user.becomes(Student)
else
user
end
end
end
但仍然没有超越current_user
日志: https://gist.github.com/anonymous/e0a5fb593b020b16a0cd2ae9d539b92a
答案 0 :(得分:2)
这对我有所帮助:
class ApplicationController < ActionController::Base
alias_method :devise_current_user, :current_user
private
def current_user
user = if params[:user].blank?
devise_current_user
else
User.find_by(email: params[:user][:email])
end
detect_role(user) if !user.blank?
end
def detect_role(user)
roles = user.roles_name
user = if roles.include?("mentor")
user.becomes(Mentor)
elsif !roles.include?("admin") && !roles.include?("mentor")
user.becomes(Student)
else
user
end
end
end
答案 1 :(得分:0)
使用
超
关键字,然后右键覆盖你的覆盖代码;
def current_user
super
----your code goes here ---
end