Api中的RuntimeError:自动加载时检测到循环依赖性

时间:2014-11-10 04:46:40

标签: ruby-on-rails ruby

您好我想为我创建一个链接(api)以查看收集的整个数据列表,因此我在控制器下创建了一个新文件夹(api),并将我的文件命名为records_controller.rb

app/controllers/api/records_controller.rb

class API::RecordsController < ApplicationController
  skip_before_filter :verify_authenticity_token
  before_action :set_headers

  def set_access
    @response.headers["Access-Control-Allow-Origin"] = "*"
  end

  def index
    @records = Record.all
    render json: @records, each_serializer: RecordSerializer
  end

#   before_action :require_user_id

#   def require_user_id
#     unless user_id
#       redirect_to :documents
#     end 
#   end
#   #user_id is correct = redirect to documents

  def create
    @records = Record.new(record_params)

    if @records.save
      render json: @records, serializer: RecordSerializer, status: 201
    else
      render json: @records.errors, status: 402
    end
  end


  private
  def record_params
    params.require(:record).permit(:record)
    end

    def set_headers
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, GET'
      headers['Access-Control-Request-Method'] = '*'
      headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    end
end
下面附带的是record.rb文件

app/models/record.rb
class Record < ActiveRecord::Base
  validates_presence_of :Number
end

这是record_serializer.rb文件

class RecordSerializer < ActiveModel::Serializer
  attributes :created_at, :Number
end

当我访问网站时:peoplecollection.herokuapp.com/api/records它给了我circular dependency detected while autoloading

请对此有所了解!

1 个答案:

答案 0 :(得分:0)

检查控制器上的命名空间。您目前有class API::RecordsController,您可能应将其更改为:class Api::RecordsController