传统的 Rails 最佳实践旨在减少控制器中的逻辑代码,因为它们设计用于路由而不是执行复杂的任务。
但是,如果您有半复杂的身份验证逻辑,您如何合理地从控制器中提取该逻辑?
在我看来,以下对于任何基本应用程序来说都是相当“标准”的逻辑。虽然逻辑与“路由”直接相关,但似乎我正在将逻辑放入控制器中并且它不是很小......我在这里会矫枉过正吗?
redirect_to ...
由于该方法只能在控制器中访问,因此甚至可以轻松地将这个逻辑提取到一个单独的类中吗?
class SessionsController < ApplicationController
# Login page POSTs here to perform authentication logic
def create
user = User.find_by(email: params[:email])
if user and user.authenticate(params[:password]) # default has_secure_password
if user.confirmed?
if user.account.active?
flash[:notice] = "Successfully logged in"
redirect_to root_path
else
flash[:error] = "This account is no longer active"
redirect_to inactive_account_path(user.account)
end
else
flash[:alert] = "You are not confirmed yet"
redirect_to confirmation_path(user.confirmation_token)
end
else
flash[:error] = "Invalid email or password"
redirect_to login_path
end
end
end