Rails:如何避免这种重定向循环?

Shp*_*ord 1 redirect routes ruby-on-rails

我想强制用户在登录时更改密码(假设他们已经将某个布尔标记设置为true).

现在我想通过做一个before_filterchange_password_check这是一个在ApplicationController.

def change_password_check
  if current_user.change_password == true
    flash[:notice] = "You must be update your password before continuing"
    redirect_to change_password_path
    return false
  end
end
Run Code Online (Sandbox Code Playgroud)

显然,问题是我得到了一个重定向循环.

那么,before_filter如果用户不在change_password_check路线上(或发布到user#update),我怎么能这样做呢?

我正在运行Rails 3.

Gaz*_*ler 5

before_filter :change_password_check, :except => :change_password
Run Code Online (Sandbox Code Playgroud)

您只能指定和除了作为before_filter的选项.这些接受一组参数,在上面的例子中,你只有一个你想要跳过的方法,但是如果你有另一个要跳过的方法(你的更新动作),你可以这样做:

before_filter :change_password_check, :except => [:change_password, :update]
Run Code Online (Sandbox Code Playgroud)

只有与之相反,所以:

before_filter :change_password_check, :only => :check_me
Run Code Online (Sandbox Code Playgroud)

只有在方法为check_me时才会运行.