从控制器中的两个不同变量绕过模型中的方法?
bypassing methods in model from two different variables in controller?
我的控制器和模型中有这段代码
会话控制器
def create
user = User.from_omniauth(env["omniauth.auth"])
user.skip_password_validation = true
unless user.present?
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
# Log the user in and redirect to the user's show page.
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
else
log_in user
redirect_to user
end
end
用户模型
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
因为我的会话控制器中有两种不同的登录方法,所以我需要跳过我的用户模型中的方法。我已经查看了一些内容,例如过滤器之前、操作之前和属性访问器之前,并搜索了网络,但似乎无法找到如何跳过模型中的方法,因此我可以为我在会话中分配的每个用户变量绕过这些方法控制器?即,一个用于 omniauth facebook,另一个用于标准登录。
如果我正确理解了这个问题,在普通身份验证的情况下,将不会设置 env["omniauth.auth"]
,因此可能会使用:
def create
user = if env["omniauth.auth"]
User.from_omniauth(env["omniauth.auth"]).tap do |u|
u.skip_password_validation = true
end
else
User.find_by(email: params[:session][:email].downcase).tap do |u|
unless u && u.authenticate(params[:session][:password])
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
return
end
end
end
log_in user
redirect_to user
end
我的控制器和模型中有这段代码
会话控制器
def create
user = User.from_omniauth(env["omniauth.auth"])
user.skip_password_validation = true
unless user.present?
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
# Log the user in and redirect to the user's show page.
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
else
log_in user
redirect_to user
end
end
用户模型
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
因为我的会话控制器中有两种不同的登录方法,所以我需要跳过我的用户模型中的方法。我已经查看了一些内容,例如过滤器之前、操作之前和属性访问器之前,并搜索了网络,但似乎无法找到如何跳过模型中的方法,因此我可以为我在会话中分配的每个用户变量绕过这些方法控制器?即,一个用于 omniauth facebook,另一个用于标准登录。
如果我正确理解了这个问题,在普通身份验证的情况下,将不会设置 env["omniauth.auth"]
,因此可能会使用:
def create
user = if env["omniauth.auth"]
User.from_omniauth(env["omniauth.auth"]).tap do |u|
u.skip_password_validation = true
end
else
User.find_by(email: params[:session][:email].downcase).tap do |u|
unless u && u.authenticate(params[:session][:password])
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
return
end
end
end
log_in user
redirect_to user
end