2016-05-13 107 views
0

我正在开发一个使用Ruby on Rails的API。我已经创造了一些规格为posts_controller.rb,当运行规范我有这个错误Rspec SystemStackError堆栈级别太深

SystemStackError: stack level too deep 
./app/controllers/api/v1/posts_controller.rb:10:in `show' 
./spec/controllers/api/v1/posts_controller_spec.rb:8:in `block (3 levels) in <top (required)>' 

这是我posts_controller_spec.rb

require 'spec_helper' 

describe API::V1::PostsController do 
    describe "GET #show" do 
    before(:each) do 
     @post = FactoryGirl.create :post 
     get :show, id: @post.id 
    end 

    it "returns the information about a post on a hash" do 
     post_response = json_response[:post] 
     expect(post_response[:description]).to eql @post.description 
    end 

    it "has the user as a embeded object" do 
     post_response = json_response[:post] 
     expect(post_response[:user][:email]).to eql @post.user.email 
    end 

    it { expect(response.status).to eql 200 } 
    end 
    . 
    . 
    . 

这是我posts_controller.rb

class API::V1::PostsController < ApplicationController 
    respond_to :json 

    def show 
    respond_with Post.find(params[:id]) 
    end 

    . 
    . 
    . 

人有想法来解决这个问题?

我意识到这是导致错误的线路,任何人都知道为什么?在post_serializer.rb文件我有这个

class PostSerializer < ActiveModel::Serializer 
    attributes :id, :description, :price, :published 
    has_one :user # this is the line !!! 
end 

如果我删除此行的问题将是固定的,但任何人都知道这是为什么?

+0

'json_response'做什么?另外,工厂有什么不寻常的吗? – zetetic

+0

'json_response'是一个帮助方法,返回这个:JSON.parse(response.body,symbolize_names:true) – crespitowil

+0

你有'UserSerializer'吗?如果是这样,请显示它的内容。 –

回答

1

您的串行器循环引用:后试图连载它的用户,但用户串行序列化用户的帖子,然后连载用户等

有关于这个问题在一个漫长的github issue active_model_serializers 0.9.x.这个问题显然固定在0.10,尽管看起来并不兼容rails 3.x

一个常见的技术似乎是有两个版本的用户序列化程序:一个包含帖子,一个不包含帖子。