我正在尝试对由 Rails API 应用程序中的 API 返回的 JSON 对象使用 Rail 的序列化程序。我需要进行身份验证、获取 API、过滤数据并重新广播过滤后的数据。不需要 View 。
为了过滤数据,我正在尝试实现序列化程序 - 很明显我没有正确设置它。我可以看到它正在正常工作,因为它在返回的数据前面添加了初始对象(请参阅下面的数据:序列化对象以 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 View ,我想知道这是否与最后一个链接中提到的问题。但是我完成的另一个教程 ( https://thesocietea.org/2015/03/building-a-json-api-with-rails-part-2-serialization/ ) 在没有 View 的 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
Controller
class KoboDataController < ApplicationController
def index
@kobo_info = KoboApi.get_api_info
render json: @kobo_info
end
end
应用程序 Controller
class ApplicationController < ActionController::API
include ActionController::Serialization
end
kobo_api_serializer.rb
class KoboApiSerializer < ActiveModel::Serializer
attributes :prikey
end
gem 文件
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]
最佳答案
我刚刚与某人交谈,他向我介绍了我遇到的问题,并非所有问题都与我在上面发布的代码有关。我在这里发布一些细节。我确信还有其他方法可以解决此问题,但这就是我们介绍它的方式。希望对您有所帮助。
主要问题:没有each_serializer
与上面发布的语言特别相关:我没有在模型的 render
语句中包含对 each_serializer
的引用。此线程中也引用了此问题: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'
第二个问题:不正确的数据库表名
但是,我创建的两个单词的表名有问题。我用驼峰命名结构创建了我的表,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 页面正确呈现。
关于ruby-on-rails - Rails Serializer 不过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37902930/