Skip to content

Commit

Permalink
Update ember-and-json-api.md (#1894)
Browse files Browse the repository at this point in the history
* Update ember-and-json-api.md

Removed ember-data adapter change to support include directives, as it's now built-in.  Updated the documentation for how to add include directives to ember store queries, and added documentation covering how to tell Rails that we are consuming and generating jsonapi data

* Fix up format for parameter restrictions

* Update ember-and-json-api.md

Updates to address comments; explain why Rails should know what format we are consuming/generating, reword introduction to include: examples, and fix render statement to specify jsonapi instead of json.
  • Loading branch information
Ikariusrb authored and NullVoxPopuli committed Sep 7, 2016
1 parent 1899437 commit 58ebf96
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions docs/integrations/ember-and-json-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,42 @@ You will also want to set the `key_transform` to `:unaltered` since you will adj
ActiveModelSerializers.config.key_transform = :unaltered
```

Lastly, in order to properly handle JSON API responses, we need to register a JSON API renderer, like so:
In order to properly handle JSON API responses, we need to register a JSON API renderer, like so:

```ruby
# config/initializers/active_model_serializers.rb
ActiveSupport.on_load(:action_controller) do
require 'active_model_serializers/register_jsonapi_renderer'
end
```
Rails also requires your controller to tell it that you accept and generate JSONAPI data. To do that, you use `respond_to` in your controller handlers to tell rails you are consuming and returning jsonapi format data. Without this, Rails will refuse to parse the request body into params. You can add `ActionController::MimeResponds` to your application controller to enable this:

```ruby
class ApplicationController < ActionController::API
include ActionController::MimeResponds
end
```
Then, in your controller you can tell rails you're accepting and rendering the jsonapi format:
```ruby
# POST /post
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.jsonapi { render jsonapi: @post, status: :created, location: @post }
else
format.jsonapi { render jsonapi: @post.errors, status: :unprocessable_entity }
end
end
end

# Only allow a trusted parameter "white list" through.
def post_params
ActiveModelSerializers::Deserialization.jsonapi_parse!(params, only: [:title, :body] )
end
end
```


### Adapter Changes

Expand All @@ -69,18 +97,6 @@ export default DS.JSONAPIAdapter.extend({
return pluralize(underscored);
},

// allows queries to be sent along with a findRecord
// hopefully Ember / EmberData will soon have this built in
// ember-data issue tracked here:
// https://github.com/emberjs/data/issues/3596
urlForFindRecord(id, modelName, snapshot) {
let url = this._super(...arguments);
let query = Ember.get(snapshot, 'adapterOptions.query');
if(query) {
url += '?' + Ember.$.param(query);
}
return url;
}
});
```

Expand All @@ -104,18 +120,15 @@ export default DS.JSONAPISerializer.extend({

```

## Including Nested Resources

Previously, `store.find` and `store.findRecord` did not allow specification of any query params.
The ActiveModelSerializers default for the `include` parameter is to be `nil` meaning that if any associations are defined in your serializer, only the `id` and `type` will be in the `relationships` structure of the JSON API response.
For more on `include` usage, see: [The JSON API include examples](./../general/adapters.md#JSON-API)
## Including Nested Resources

With the above modifications, you can execute code as below in order to include nested resources while doing a find query.
Ember Data can request related records by using `include`. Below are some examples of how to make Ember Data request the inclusion of related records. For more on `include` usage, see: [The JSON API include examples](./../general/adapters.md#JSON-API)

```javascript
store.findRecord('post', postId, { adapterOptions: { query: { include: 'comments' } } });
store.findRecord('post', postId, { include: 'comments' } );
```
will generate the path `/posts/{postId}?include='comments'`
which will generate the path /posts/{postId}?include='comments'

So then in your controller, you'll want to be sure to have something like:
```ruby
Expand Down

0 comments on commit 58ebf96

Please sign in to comment.