首页 > 解决方案 > Rails 重定向操作:索引,不起作用

问题描述

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

我已经这样做了:

  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-railsruby

解决方案


问题是你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

推荐阅读