Hi I wanted to create a link (an api) for me to see the whole list of data collected, hence I created a new folder (api) under controllers, and named my file records_controller.rb
嗨我想为我创建一个链接(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
attached below is the record.rb
file
附在下面的是record.rb文件
app/models/record.rb
class Record < ActiveRecord::Base
validates_presence_of :Number
end
And this is the record_serializer.rb
file
这是record_serializer.rb文件
class RecordSerializer < ActiveModel::Serializer
attributes :created_at, :Number
end
when i went to the website: peoplecollection.herokuapp.com/api/records
it gave me circular dependency detected while autoloading
当我去网站:peoplecollection.herokuapp.com/api/records它给了我自动加载时检测到循环依赖
Please shed some light on this!
请详细说明一下!
1 个解决方案
#1
0
Check your namespacing on the controller. You currently have class API::RecordsController
and you probably should change it to: class Api::RecordsController
检查控制器上的命名空间。您目前有API :: RecordsController类,您可能应该将其更改为:class Api :: RecordsController
#1
0
Check your namespacing on the controller. You currently have class API::RecordsController
and you probably should change it to: class Api::RecordsController
检查控制器上的命名空间。您目前有API :: RecordsController类,您可能应该将其更改为:class Api :: RecordsController