我有一个bookmarks
资源,默认情况下已将其映射为json
我的api
名称空间下的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
ArgumentError in Api::UsersController#index
方法有效,但索引方法为我wrong number of arguments (1 for 0)
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
更新:完整堆栈跟踪:https://gist.github.com/amite/b79fc42bfd73de5a07bd
截图
这是序列化器:
index
关于堆栈溢出的其他解决方案,我还尝试了其他版本的 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]}.
这给了我错误:
index
接下来我的def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
ActiveModel::ArraySerializer.new(bookmarks, each_serializer: ::BookmarkSerializer).to_json
end
方法的最后一个版本如下所示:
uninitialized constant ActiveModel::ArraySerializer
这给了我错误rails 4.1.5
我做错了什么?我正在使用active_model_serializers
和gem 'active_model_serializers', github: 'rails-api/active_model_serializers'
的github版本
宝石。
bookmarks
更新:由于我正在尝试输出BookmarksSerializer
的集合,我还尝试使用单独的序列化程序
ArgumentError in Api::UsersController#index
wrong number of arguments (1 for 0)
但我收到同样的错误:index
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
# 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]
它只输出默认哈希
Full Gemfile
{{1}}
答案 0 :(得分:5)
结果我需要包含宝石的v0.9
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-9-stable'
答案 1 :(得分:2)
你可以尝试改变:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
bookmarks.map { |bookmark| ::BookmarkSerializer.new(bookmark)}.to_json #updated line
end
为:
def index
user = User.find_by(username: params[:username])
bookmarks = user.bookmarks
render json: bookmarks.map { |bookmark| ::BookmarkSerializer.new(bookmark)}
end