繁体   English   中英

JSON:Rails中的循环引用错误

[英]JSON:Circular Reference Error in Rails

我正在尝试使用rails创建一个RESTful API。 当我尝试http://localhost:3000/api/v1/projects.json?token=kS8xi7YzaUDEmbmi9XL6出现错误:

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'

基本控制器

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

主控制器

class Api::V1::ProjectsController < Api::V1::BaseController 
 def index
   respond_with(Project.for(current_user))
  end
 end

在控制台中尝试时, User.find_by_authentication_token("kS8xi7YzaUDEmbmi9XL6")提供了正确的用户。 当通过参数发送相同的令牌时,它将进入该隐式错误。 我在这里想念什么?

谢谢

得到它了。 我需要将控制器动作修复为

def index
  respond_with(Project.for(current_user).all)
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM