mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Move SerializableResource to ActiveModelSerializers namespace
Ref. https://github.com/rails-api/active_model_serializers/pull/1310
This commit is contained in:
@@ -14,7 +14,7 @@ post = Post.create(title: "Sample post", body: "I love Active Model Serializers!
|
||||
options = {}
|
||||
|
||||
# Create a serializable resource instance
|
||||
serializable_resource = ActiveModel::SerializableResource.new(post, options)
|
||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
|
||||
|
||||
# Convert your resource into json
|
||||
model_json = serializable_resource.as_json
|
||||
@@ -38,20 +38,20 @@ serializer = ActiveModel::Serializer.serializer_for(post, options)
|
||||
You could also retrieve the serializer via:
|
||||
|
||||
```ruby
|
||||
ActiveModel::SerializableResource.new(post, options).serializer
|
||||
ActiveModelSerializers::SerializableResource.new(post, options).serializer
|
||||
```
|
||||
|
||||
Both approaches will return an instance, if any, of the resource's serializer.
|
||||
|
||||
## Serializing before controller render
|
||||
|
||||
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModel::SerializableResource` with
|
||||
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModelSerializers::SerializableResource` with
|
||||
the resource you want to be serialized and call `.as_json`.
|
||||
|
||||
```ruby
|
||||
def create
|
||||
message = current_user.messages.create!(message_params)
|
||||
message_json = ActiveModel::SerializableResource.new(message).as_json
|
||||
message_json = ActiveModelSerializers::SerializableResource.new(message).as_json
|
||||
MessageCreationWorker.perform(message_json)
|
||||
head 204
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user