Response_with requests location on error

I have a pretty standard authentication method

private def authenticate_user @current_user = User.find_by_authentication_token(params[:token]) unless @current_user error = { :error => "Invalid token." } respond_with(error, :status => 401 ) end end 

I am calling the API to verify that authentication failed.

I get an error

 ArgumentError (Nil location provided. Can't build URI.): app/controllers/api/v1/base_controller.rb:13:in `authenticate_user' 

What am I doing wrong?

+7
source share
2 answers

Due to the nature of your error, I assume that authenticate_user is called part of the create action.

If so, I believe that the answer I provided here will also help you.

Assuming, however, that this is part of creating an authenticated session, that is, there is no actual location for the newly created โ€œresource,โ€ I would set zero for the location of the response, as in:

 ... respond_with(error, :status => 401, :location => nil) ... 

This will make more sense if you look at the related answer. If this still does not make sense, I will be happy to clarify.

+7
source

I changed response_with to render and it worked:

 render json: { success: false, message: "an error" }, status: 500 
0
source

All Articles