Sha256: 63f1e5da3e83fe269f20669dff61570ac25a95fb703923fad41cf97ef9652161

Contents?: true

Size: 1.31 KB

Versions: 12

Compression:

Stored size: 1.31 KB

Contents

# Getting Started

## Installation

### ActiveModel::Serializer is already included on Rails >= 5

Add this line to your application's Gemfile:

```
gem 'active_model_serializers'
```

And then execute:

```
$ bundle
```

## Creating a Serializer

The easiest way to create a new serializer is to generate a new resource, which
will generate a serializer at the same time:

```
$ rails g resource post title:string body:string
```

This will generate a serializer in `app/serializers/post_serializer.rb` for
your new model. You can also generate a serializer for an existing model with
the serializer generator:

```
$ rails g serializer post
```

The generated seralizer will contain basic `attributes` and
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:

```ruby
class PostSerializer < ActiveModel::Serializer
  attributes :title, :body

  has_many :comments
  has_one :author

end
```

and

```ruby
class CommentSerializer < ActiveModel::Serializer
  attributes :name, :body

  belongs_to :post_id

end
```

## Rails Integration

AMS will automatically integrate with you Rails app, you won't need to update your controller, this is a example of how it will look like:

```ruby
class PostsController < ApplicationController

  def show
    @post = Post.find(params[:id])
    render json: @post
  end

end
```

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
active_model_serializers-0.10.0.rc3 docs/general/getting_started.md
cheap_ams-0.10.11 docs/general/getting_started.md
cheap_ams-0.10.10 docs/general/getting_started.md
cheap_ams-0.10.8 docs/general/getting_started.md
cheap_ams-0.10.7 docs/general/getting_started.md
cheap_ams-0.10.6 docs/general/getting_started.md
cheap_ams-0.10.5 docs/general/getting_started.md
cheap_ams-0.10.4 docs/general/getting_started.md
cheap_ams-0.10.3 docs/general/getting_started.md
cheap_ams-0.10.2 docs/general/getting_started.md
cheap_ams-0.10.1 docs/general/getting_started.md
cheap_ams-0.10.0.rc2 docs/general/getting_started.md