respond_with is asking for location on error
Asked Answered
T

2

8

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?

Toothwort answered 20/10, 2011 at 6:30 Comment(2)
Is this the code in base_controller.rb? Which line is line 13?Teryn
The method is private so we need to know where/how it is called. Please show how/where you are calling this method in the controller.Cadent
V
8

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.

Vallie answered 15/11, 2011 at 12:36 Comment(2)
Hi, I did this and still the same problem, respond_with(:ec => 200, :em => "None", :places => @places,:location => nil)Proximate
@Proximate That would be very difficult considering I know nothing about your exact problem. Why not open another question if nothing on the site seems to help you specific predicament?Vallie
S
0

I changed respond_with to render and it worked:

render json: { success: false, message: "an error" }, status: 500
Stereoisomer answered 10/9, 2013 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.