Rails重定向动作:索引,不起作用

问题描述 投票:1回答:1

我正在寻找解决方案。当他们不是管理员时,我正在尝试重定向用户。

我做到了:

  def is_admin?
    if current_user.admin?
      redirect_to action: "index"
    else
      redirect_to root_path
    end
  end

我在before_action :is_admin?posts_controller

我不知道为什么,但重定向不起作用。 Firefox给了我一个空白页面:

页面未正确重定向

谢谢你的帮助

ruby-on-rails ruby
1个回答
2
投票

问题是你的before_action :is_admin?在索引方法上调用is_admin?并继续重定向...

我不明白为什么重定向'索引',你应该改变你的方法is_admin?像:

  def is_admin?
    # redirect ONLY if user isn't admin
    redirect_to root_path unless current_user.admin?
  end
© www.soinside.com 2019 - 2024. All rights reserved.