srnvs
srnvs

Reputation: 1077

Rails 4.2 redirect status 200?

I'm writing an application in Rails 4.2 and my controller is as follows:

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

so when I use a rest client to send request to show or new before signing in, I'm being redirected to the signin page which is what I want and on signing in as a non-admin, I'm being redirected to the root path, which is also what I want, however, in both cases, the status code is 200 (should it not be 302?) which is quite unexpected, I'm trying to write tests for the controller and I'm having a hard time testing the redirects, the tests fail with Expected <redirect>, got <200> Can anyone please help me in pointing out to what's going on here?

I'm using devise for authentication

Upvotes: 0

Views: 873

Answers (1)

Michal
Michal

Reputation: 753

It should be 302, as it is default status for redirect_to (http://apidock.com/rails/ActionController/Base/redirect_to) but you can harcode it as well:

redirect_to root_path, status: 302

Upvotes: 1

Related Questions