Sha256: 8979ad54dd39458969b5099737f43e11e1a6e7b79a22231d4fdc6c55c13dcda7
Contents?: true
Size: 1.11 KB
Versions: 1
Compression:
Stored size: 1.11 KB
Contents
## 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.
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_model_serializers-0.10.0.rc3 | docs/howto/outside_controller_use.md |