ActiveModel Serializers: How do I serialize a collection of resources?

I have a resource bookmarksand it mapped it for the jsondefault service in my namespace api, as it was in mine routes.rb:

  namespace :api, defaults: {format: 'json'} do
    resources :bookmarks
    get ':username', to: 'users#index'
    get ':username/bookmarks/:id', to: 'users#show'
  end

I have a controller Api::UsersControllerand a supporting BookmarkSerializerone that works fine in a separate bookmark resource, e.g.http://localhost:3000/api/emma_carter/bookmarks/87

But when I try to click http://localhost:3000/api/emma_carter, which should serve all the bookmarks owned by the user, I get all kinds of errors. Here is myApi::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

The method showworks, but the index method gives meArgumentError in Api::UsersController#index

wrong number of arguments (1 for 0)

UPDATE: Full stack trace here: https://gist.github.com/amite/b79fc42bfd73de5a07bd

screenshot

enter image description here

Here is the serializer:

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

, 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 github active_model_serializers .

gem 'active_model_serializers', github: 'rails-api/active_model_serializers'

UPDATE: bookmarks, BookmarksSerializer : ArgumentError in Api::UsersController#index wrong number of arguments (1 for 0)

UPDATE2: index, , 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

source '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]
+4
2

v0.9

gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-9-stable'
+5

:

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
+2

All Articles