-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1155 from CodedBeardedSignedTaylor/documentation
Outside controller use tutorial
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
## Using AMS Outside Of A Controller | ||
|
||
### Serializing a resource | ||
|
||
In AMS versions 0.10 or later, serializing resources outside of the controller context is fairly simple: | ||
|
||
```ruby | ||
# Create our resource | ||
post = Post.create(title: "Sample post", body: "I love Active Model Serializers!") | ||
|
||
# Optional options parameters | ||
options = {} | ||
|
||
# Create a serializable resource instance | ||
serializable_resource = ActiveModel::SerializableResource.new(post, options) | ||
|
||
# Convert your resource into json | ||
model_json = serializable_resource.as_json | ||
``` | ||
|
||
### Retrieving a Resource's Active Model Serializer | ||
|
||
If you want to retrieve a serializer for a specific resource, you can do the following: | ||
|
||
```ruby | ||
# Create our resource | ||
post = Post.create(title: "Another Example", body: "So much fun.") | ||
|
||
# Optional options parameters | ||
options = {} | ||
|
||
# Retrieve the default serializer for posts | ||
serializer = ActiveModel::Serializer.serializer_for(post, options) | ||
``` | ||
|
||
You could also retrieve the serializer via: | ||
|
||
```ruby | ||
ActiveModel::SerializableResource.new(post, options).serializer | ||
``` | ||
|
||
Both approaches will return an instance, if any, of the resource's serializer. |