我正在编写Rails 4.2中的应用程序,我的控制器如下:
class PostsController < ApplicationController
before_action :require_admin, :except => [:index]
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def require_admin
unless user_signed_in? && current_user.admin?
flash[:alert] = 'You require admin privileges for editing posts'
redirect_to root_path
end
end
因此,当我使用REST客户端发送请求以显示或新的请求之前,我将被重定向到标志页面,这是我想要的,并以非应用程序登录时,我将被重定向对于根路径,这也是我想要的,但是,在这两种情况下,状态代码是200(不是302?),这是非常意外的,我试图为控制器编写测试,而我是很难测试重定向,测试失败了Expected <redirect>, got <200>
谁能帮助我指出这里发生的事情?
我正在使用设计进行身份验证
应该为302,因为它是redirect_to(http://apidock.com/rails/actioncontroller/base/redirect_to)的默认状态,但是您也可以harcode:
redirect_to root_path, status: 302