我试图在Rails API应用程序中的API返回的JSON对象上使用Rails的序列化程序。我需要进行身份验证,摄入API,过滤数据,并重新广播过滤后的数据。不需要查看。
为了过滤数据,我尝试实现序列化器--很明显,我没有正确地设置它。我可以看到它正在正常工作,因为它在返回的数据前面添加了初始对象(参见下面的数据:序列化对象以kobo_data开头)。但是它返回了所有的数据字段,而不仅仅是我试图调用的字段。在本例中,我试图只返回字段prikey,但它正在返回所有的数据字段。
我试图在类似的线程(ActiveModel::Serializer in Rails - serializer methods ignored in JSON result、rails active model serializer not doing anything、Using ActiveModel::Serializer in Rails - JSON data differs between json and index response)中解决问题,尽管由于我没有.html视图,我想知道这是否与上一个链接中提到的问题有关。但是我所做的另一个教程(https://thesocietea.org/2015/03/building-a-json-api-with-rails-part-2-serialization/)在没有视图的Rails-API应用程序中使用序列化,所以我猜它不是。
关于我做错了什么以及如何解决它,有什么建议吗?
模型:
class KoboApi < ActiveRecord::Base
require 'rest_client'
USERNAME = ENV['KOBO_API_USER']
PASSWORD = ENV['KOBO_API_PWD']
SURVEY = ENV['KOBO_API_SURVEY']
# API_BASE_URL = "https://kc.kobotoolbox.org/api/v1/"
API_BASE_URL = "https://sg.smap.com.au/api/v1"
def self.get_api_info
uri = "#{API_BASE_URL}/data/#{SURVEY}?format=json"
rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD)
response = rest_resource.get
JSON.parse response
end
end控制器
class KoboDataController < ApplicationController
def index
@kobo_info = KoboApi.get_api_info
render json: @kobo_info
end
end应用控制器
class ApplicationController < ActionController::API
include ActionController::Serialization
endkobo_api_serializer.rb
class KoboApiSerializer < ActiveModel::Serializer
attributes :prikey
endGem文件
source 'https://rubygems.org'
gem 'active_model_serializers', '~> 0.8.3'
gem 'rest-client'
gem 'pg'
gem 'rails', '4.2.5.1'
gem 'rails-api'
gem 'spring', :group => :development
# gem for environment variables
gem 'dotenv-rails', :groups => [:development, :test]
gem 'figaro'数据返回
{"kobo_data":[{"prikey":"1","Upload Time":"2016-06-17 11:11:38.802+00","Version":"1","Complete":"t","Survey Notes":"","Location Trigger":"","deviceid":"webworm"....[data truncated]发布于 2016-06-20 07:43:53
我刚和一个人谈过我遇到的问题,并不是所有的问题都与我上面发布的代码有关。我在这里张贴一些细节。我相信还有其他方法可以解决这个问题,但我们就是这样做的。希望能帮上忙。
主要问题:没有each_serializer
与上面发布的语言特别相关:我在我的模型中的render语句中没有包含对render的引用。这个问题也在这里的线程中引用:rails active model serializer not doing anything。
控制器
class KoboDataController < ApplicationController
def index
@kobo_info = KoboApi.get_api_info
@kobo_info.each do |kobo_info|
begin
#this pulls in new records that have been added to the api and adds them to the database. it doesn't pull in records from the api that are already in the db
KoboApi.find_or_create_by(lemurs_quantity: kobo_info['lemurs_quantity'], month_and_year: Date.parse(kobo_info['month_and_year']))
# some of my records don't have valid date values, so `rescue` is added to keep rails from returning a `notfound` page.
# quick tutorial here: http://www.rubytutorial.io/rails-rescue_from/
rescue
puts "rescued"
end
end
# I needed to update my render logic.
# see addition of `each_serializer` here.
# @kobo_info has also changed to KoboApi.all as well.
render(
json: KoboApi.all,
each_serializer: KoboApiSerializer
)
end
end如果我的数据库配置正确,这应该已经为我修复了它。
此外,为了防止我使用的Active Model Serializers出现问题,我们更新了我的gem文件,用以下内容替换了我的行(gem 'active_model_serializers', '~> 0.8.3'):
gem 'active_model_serializers', :git => 'git://github.com/AskNative/active_model_serializers.git', :branch => '0-8-stable' 第二个问题:不正确的db表名称
然而,我对我创建的两个单词的表名有一个问题。我创建了具有camelCase名称结构的表,rails期望使用separated_words (即:带有下划线)。
您可以看到这里的表名是koboApis
原始迁移
class KoboApis < ActiveRecord::Migration
def change
create_table :koboApis do |t|
t.integer :lemurs_quantity
t.date :month_and_year
t.text :_geolocation
t.text :lemur_category
end
end
end在我的原始模式中,您可以看到表名确实是kobo_apis,而不是“`koboApis”。
模式
ActiveRecord::Schema.define(version: 20160620063545) do
create_table "kobo_apis", force: :cascade do |t|
t.integer "lemurs_quantity"
t.date "month_and_year"
t.text "_geolocation"
t.text "lemur_category"
end
end由于这一点,表Rails所期待的根本不存在。
我们通过在终端运行第二次迁移来解决这个问题:rails g migration RenameOldTableToNewTable。然后,我将这个逻辑添加到新的迁移文件:rename_table :koboApis, :kobo_apis中。这里的整个迁移文件:
更新迁移文件
class RenameOldTableToNewTable < ActiveRecord::Migration
def change
rename_table :koboApis, :kobo_apis
end
end我运行了rake db:drop db:create db:migrate来更新所有内容,然后正确地呈现了Rails Api页面。
https://stackoverflow.com/questions/37902930
复制相似问题