从控制器阻止范围的所有路由

时间:2013-01-09 06:21:30

标签: ruby-on-rails ruby-on-rails-3 devise

我正在尝试访问用户登录的条件,如果他的帐户被停用,他/她应该被重定向回登录页面,并带有flash消息我正在尝试这样的事情

def after_sign_in_path_for(resource)
 if resource.group.slug == 'a'
   @u = User.find_by_email(resource.email)
   if @u.member.state_id == "someid"
     '/logout'
     flash[:notice]= "My message."
   else
     a_root_path
   end
 elsif resource.group.slug == 'b'
   b_root_path
 elsif resource.group.slug == 'c'
   c_root_path
 else
   new_user_session_path
 end

end

有什么建议我如何阻止用户登录并阻止该范围的所有其他路由?

2 个答案:

答案 0 :(得分:0)

after_sign_in_path_for方法的返回值应该是您要重定向到的URL。因此,您不需要自己调用redirect_to。

if @u.member.state_id == "someid"
  flash[:notice]= "My message."
  '/logout'
else
  a_root_path
end

(未经测试)

答案 1 :(得分:0)

为什么不注销用户?如下

def after_sign_in_path_for(resource)
 if resource.group.slug == 'a'
  @u = User.find_by_email(resource.email)
  if @u.member.state_id == "someid"
    sign_out resource
    '/users/sign_in' (or root_path based on your requirement)
    flash[:notice]= "My message."
  else
    a_root_path
  end
 elsif resource.group.slug == 'b'
  b_root_path
 elsif resource.group.slug == 'c'
  c_root_path
 else
  new_user_session_path
 end