respond_with is asking for location on error

5.4k views Asked by At

I have a pretty standard authenticate 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 ensure the authenticate fails.

I get an error stating

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

What am I doing wrong?

2

There are 2 answers

2
Aubergine On BEST ANSWER

By the specific flavor of your error, I am guessing that "authenticate_user" is called as part of a "create" action.

If that is the case, I believe the answer I provided here will help you as well.

Assuming, however, that this is part of creating an authenticated session, meaning there is no actual location for the newly created "resource", I would supply nil for the response location, as in:

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

That will make more sense once you have a look at the linked answer. If it still doesn't make sense, I'll be happy to clarify.

0
Tomas Romero On

I changed respond_with to render and it worked:

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