这是我所做的,我觉得这不是一个好主意,而且它是反模式:
经过身份验证的用户根据用户状态被定向到家庭控制器和控制器中,我将他们定向到不同的路由。如果我这样做会很糟糕:
if user.status == "accepted"
redirect_to ### some view
else
redirect_to ### another view
end
我考虑过使用CanCan
,但我认为这可能是矫枉过正
这是我所做的,我觉得这不是一个好主意,而且它是反模式:
经过身份验证的用户根据用户状态被定向到家庭控制器和控制器中,我将他们定向到不同的路由。如果我这样做会很糟糕:
if user.status == "accepted"
redirect_to ### some view
else
redirect_to ### another view
end
我考虑过使用CanCan
,但我认为这可能是矫枉过正
It depends on a case. It could be acceptable. I can't give advice as that would be expressing an opinion, however, I can give you an alternative since you are clearly interested in how to do this differently.
When working with Sorcery authentication gem (and as far as I know, Devise offers the same approach) one applies a filter to a controller, that redirects any non-logged-in user to a page you specify (it's sensible to redirect to login page).
This is implemented using a before_filter
(same as before_action
, really). Methods fed into it are built up into a chain that is called on every controller's action (unless parameters state otherwise). If any of the filter methods redirect
s or render
s, chain execution is halted. This can be seen in the server's logs.
This approach works really well if you need to apply that same condition and redirect to multiple actions. If it's not the case and it's the only place where you do this check, your current implementation may be just enough.