mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
commit
35e17b5964
14
README.md
14
README.md
@ -29,7 +29,7 @@ Given two models, a `Post(title: string, body: text)` and a
|
||||
`Comment(name:string, body:text, post_id:integer)`, you will have two
|
||||
serializers:
|
||||
|
||||
```
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
attribute :title, :body
|
||||
|
||||
@ -41,7 +41,7 @@ end
|
||||
|
||||
and
|
||||
|
||||
```
|
||||
```ruby
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
attribute :name, :body
|
||||
|
||||
@ -56,7 +56,7 @@ serializer classes. By default, they will use the JsonApiAdapter, implemented
|
||||
by AMS. If you want to use a different adapter, such as a HalAdapter, you can
|
||||
change this in an initializer:
|
||||
|
||||
```
|
||||
```ruby
|
||||
ActiveModel::Serializer.default_adapter = ActiveModel::Serializer::Adapter::HalAdapter
|
||||
```
|
||||
|
||||
@ -83,11 +83,15 @@ it exists, use it to serialize the `Post`.
|
||||
|
||||
Add this line to your application's Gemfile:
|
||||
|
||||
```
|
||||
gem 'active_model_serializers'
|
||||
```
|
||||
|
||||
And then execute:
|
||||
|
||||
```
|
||||
$ bundle
|
||||
```
|
||||
|
||||
## Creating a Serializer
|
||||
|
||||
@ -109,7 +113,7 @@ $ rails g serializer post
|
||||
The generated seralizer will contain basic `attributes` and
|
||||
`has_many`/`belongs_to` declarations, based on the model. For example:
|
||||
|
||||
```
|
||||
```ruby
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
attribute :title, :body
|
||||
|
||||
@ -121,7 +125,7 @@ end
|
||||
|
||||
and
|
||||
|
||||
```
|
||||
```ruby
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
attribute :name, :body
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user