I’m trying to make a RESTful API with rails. When I try http://localhost:3000/api/v1/projects.json?token=kS8xi7YzaUDEmbmi9XL6 I get a error:
Parameters: {"token"=>"kS8xi7YzaUDEmbmi9XL6"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."authentication_token" = 'kS8xi7YzaUDEmbmi9XL6' LIMIT 1
Completed 500 Internal Server Error in 3ms
ActiveSupport::JSON::Encoding::CircularReferenceError (object references itself):
app/controllers/api/v1/projects_controller.rb:3:in `index'
The base controller
class Api::V1::BaseController < ActionController::Base
before_filter :authenticate_user
respond_to :json
private
def authenticate_user
@current_user = User.find_by_authentication_token(params[:token])
unless @current_user
respond_with( {:error => "Token is invalid."} )
end
end
def current_user
@current_user
end
end
The main controller
class Api::V1::ProjectsController < Api::V1::BaseController
def index
respond_with(Project.for(current_user))
end
end
On trying in the console, User.find_by_authentication_token("kS8xi7YzaUDEmbmi9XL6") gives the correct user. When sending this same token via params it goes into that cryptic error. What am I missing here?
Thanks
Got it. I needed to do fix the controller action to