ruby-on-rails – ActiveModel序列化程序:如何序列化资源集合?
我有一个书签资源,并将其映射为默认情况下在我的api名称空间下提供json,就像我的routes.rb中一样:
namespace :api,defaults: {format: 'json'} do resources :bookmarks get ':username',to: 'users#index' get ':username/bookmarks/:id',to: 'users#show' end 我有一个Api :: UsersController控制器和一个支持的BookmarkSerializer,可以在单个书签资源上正常工作,如http:// localhost:3000 / api / emma_carter / bookmarks / 87 但是当我尝试点击http:// localhost:3000 / api / emma_carter时,它应该为用户拥有的所有书签提供服务,我会遇到各种不同的错误.这是我的Api :: UsersController module Api class UsersController < ApplicationController respond_to :json def index user = User.find_by(username: params[:username]) bookmarks = user.bookmarks render json: bookmarks end def show user = User.find_by(username: params[:username]) bookmark = user.bookmarks.find_by(params[:id]) render json: bookmark end end end show方法有效但索引方法在Api :: UsersController#index中给出了ArgumentError wrong number of arguments (1 for 0) 更新:完整堆栈跟踪:https://gist.github.com/amite/b79fc42bfd73de5a07bd 截图 这是序列化器: class BookmarkSerializer < ActiveModel::Serializer attributes :id,:url,:title,:domain,:notes,:image,:created,:username belongs_to :user def created object.created_at end def username user.username end end 看看堆栈溢出的其他解决方案,我也尝试过其他版本的索引方法: def index user = User.find_by(username: params[:username]) bookmarks = user.bookmarks bookmarks.map { |bookmark| ::BookmarkSerializer.new(bookmark)}.to_json #updated line end 这给了我错误: Missing template api/users/index,application/index with {:locale=>[:en],:formats=>[:json],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:coffee,:jbuilder]}. 接下来,我的索引方法的最后一个版本如下所示: def index user = User.find_by(username: params[:username]) bookmarks = user.bookmarks ActiveModel::ArraySerializer.new(bookmarks,each_serializer: ::BookmarkSerializer).to_json end 这给了我未初始化的常量ActiveModel :: ArraySerializer错误 我究竟做错了什么?我正在使用rails 4.1.5和active_model_serializers的github版本 gem 'active_model_serializers',github: 'rails-api/active_model_serializers' 更新:因为我试图输出书签集合,我也尝试使用单独的序列化程序 UPDATE2:这是索引方法的一个版本,它在某种意义上有效,它以json格式呈现资源集合: def index user = User.find_by(username: params[:username]) bookmarks = user.bookmarks respond_with bookmarks.to_json end 但是这仍然没有使用BookmarksSerializer class BookmarksSerializer < ActiveModel::Serializer attributes :id,:title end 它只输出默认哈希 完整的Gemfile 来源’https://rubygems.org‘ # Bundle edge Rails instead: gem 'rails',github: 'rails/rails' gem 'rails','4.1.5' # Use mysql as the database for Active Record gem 'mysql2' # Use SCSS for stylesheets gem 'sass-rails','~> 4.0.3' # Use Uglifier as compressor for JavaScript assets gem 'uglifier','>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails','~> 4.0.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer',platforms: :ruby gem 'bourbon' gem 'neat' gem 'bitters' gem 'refills' gem 'wisper' gem 'rails-ioc' gem 'reform' gem 'cells' gem "pundit" gem 'active_model_serializers',github: 'rails-api/active_model_serializers' gem "font-awesome-rails" gem 'simple_form' # Use jquery as the JavaScript library gem 'jquery-rails' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder','~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc','~> 0.4.0',group: :doc group :development,:test do # Call 'debugger' anywhere in the code to stop execution and get a debugger console gem 'byebug' gem 'ffaker' gem 'pry-rails' gem 'capybara' gem 'factory_girl_rails' gem 'database_cleaner' gem 'capybara-webkit' gem 'rspec-cells' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-commands-rspec' gem 'rspec-rails' gem 'guard-rspec' gem 'rb-fsevent' if `uname` =~ /Darwin/ end # Use ActiveModel has_secure_password gem 'bcrypt','~> 3.1.7' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails',group: :development # Use debugger # gem 'debugger',group: [:development,:test] 解决方法
结果我需要包含宝石的v0.9
gem 'active_model_serializers',github: 'rails-api/active_model_serializers',branch: '0-9-stable' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |