mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
ruby syntax highlights
This commit is contained in:
parent
e7dbea383b
commit
e8727ae272
34
README.md
34
README.md
@ -29,10 +29,10 @@ Given two models, a `Post(title: string, body: text)` and a
|
|||||||
`Comment(name:string, body:text, post_id:integer)`, you will have two
|
`Comment(name:string, body:text, post_id:integer)`, you will have two
|
||||||
serializers:
|
serializers:
|
||||||
|
|
||||||
```
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
attribute :title, :body
|
attribute :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
|
||||||
url :post
|
url :post
|
||||||
@ -41,12 +41,12 @@ end
|
|||||||
|
|
||||||
and
|
and
|
||||||
|
|
||||||
```
|
```ruby
|
||||||
class CommentSerializer < ActiveModel::Serializer
|
class CommentSerializer < ActiveModel::Serializer
|
||||||
attribute :name, :body
|
attribute :name, :body
|
||||||
|
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
|
|
||||||
url [:post, :comment]
|
url [:post, :comment]
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -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
|
by AMS. If you want to use a different adapter, such as a HalAdapter, you can
|
||||||
change this in an initializer:
|
change this in an initializer:
|
||||||
|
|
||||||
```
|
```ruby
|
||||||
ActiveModel::Serializer.default_adapter = ActiveModel::Serializer::Adapter::HalAdapter
|
ActiveModel::Serializer.default_adapter = ActiveModel::Serializer::Adapter::HalAdapter
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -82,12 +82,16 @@ it exists, use it to serialize the `Post`.
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Add this line to your application's Gemfile:
|
Add this line to your application's Gemfile:
|
||||||
|
|
||||||
gem 'active_model_serializers'
|
```
|
||||||
|
gem 'active_model_serializers'
|
||||||
|
```
|
||||||
|
|
||||||
And then execute:
|
And then execute:
|
||||||
|
|
||||||
$ bundle
|
```
|
||||||
|
$ bundle
|
||||||
|
```
|
||||||
|
|
||||||
## Creating a Serializer
|
## Creating a Serializer
|
||||||
|
|
||||||
@ -109,10 +113,10 @@ $ rails g serializer post
|
|||||||
The generated seralizer will contain basic `attributes` and
|
The generated seralizer will contain basic `attributes` and
|
||||||
`has_many`/`belongs_to` declarations, based on the model. For example:
|
`has_many`/`belongs_to` declarations, based on the model. For example:
|
||||||
|
|
||||||
```
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
attribute :title, :body
|
attribute :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
|
||||||
url :post
|
url :post
|
||||||
@ -121,12 +125,12 @@ end
|
|||||||
|
|
||||||
and
|
and
|
||||||
|
|
||||||
```
|
```ruby
|
||||||
class CommentSerializer < ActiveModel::Serializer
|
class CommentSerializer < ActiveModel::Serializer
|
||||||
attribute :name, :body
|
attribute :name, :body
|
||||||
|
|
||||||
belongs_to :post_id
|
belongs_to :post_id
|
||||||
|
|
||||||
url [:post, :comment]
|
url [:post, :comment]
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user