簡體   English   中英

Rails 4.2重定向狀態為200?

[英]Rails 4.2 redirect status 200?

我正在用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 Client在登錄之前發送顯示或新請求時,我將被重定向到我想要的登錄頁面,並以非管理員身份登錄時,我將重定向到根目錄路徑,這也是我想要的路徑,但是,在兩種情況下,狀態碼都是200(應該不是302嗎?),這是非常意外的,我正在嘗試為控制器編寫測試,並且很難在測試重定向時,測試失敗,並出現Expected <redirect>, got <200> 。有人可以幫助我指出這里發生的情況嗎?

我正在使用devise進行身份驗證

它應該是302,因為它是redirect_to( http://apidock.com/rails/ActionController/Base/redirect_to )的默認狀態,但是您也可以對其進行編碼:

redirect_to root_path, status: 302

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM