Skip to content

Commit 58ebf96

Browse files
IkariusrbNullVoxPopuli
authored andcommitted
Update ember-and-json-api.md (#1894)
* 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.
1 parent 1899437 commit 58ebf96

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

docs/integrations/ember-and-json-api.md

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,42 @@ You will also want to set the `key_transform` to `:unaltered` since you will adj
3838
ActiveModelSerializers.config.key_transform = :unaltered
3939
```
4040

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

4343
```ruby
4444
# config/initializers/active_model_serializers.rb
4545
ActiveSupport.on_load(:action_controller) do
4646
require 'active_model_serializers/register_jsonapi_renderer'
4747
end
4848
```
49+
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:
50+
51+
```ruby
52+
class ApplicationController < ActionController::API
53+
include ActionController::MimeResponds
54+
end
55+
```
56+
Then, in your controller you can tell rails you're accepting and rendering the jsonapi format:
57+
```ruby
58+
# POST /post
59+
def create
60+
@post = Post.new(post_params)
61+
respond_to do |format|
62+
if @post.save
63+
format.jsonapi { render jsonapi: @post, status: :created, location: @post }
64+
else
65+
format.jsonapi { render jsonapi: @post.errors, status: :unprocessable_entity }
66+
end
67+
end
68+
end
69+
70+
# Only allow a trusted parameter "white list" through.
71+
def post_params
72+
ActiveModelSerializers::Deserialization.jsonapi_parse!(params, only: [:title, :body] )
73+
end
74+
end
75+
```
76+
4977

5078
### Adapter Changes
5179

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

72-
// allows queries to be sent along with a findRecord
73-
// hopefully Ember / EmberData will soon have this built in
74-
// ember-data issue tracked here:
75-
// https://github.com/emberjs/data/issues/3596
76-
urlForFindRecord(id, modelName, snapshot) {
77-
let url = this._super(...arguments);
78-
let query = Ember.get(snapshot, 'adapterOptions.query');
79-
if(query) {
80-
url += '?' + Ember.$.param(query);
81-
}
82-
return url;
83-
}
84100
});
85101
```
86102

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

105121
```
106122

107-
## Including Nested Resources
108123

109-
Previously, `store.find` and `store.findRecord` did not allow specification of any query params.
110-
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.
111-
For more on `include` usage, see: [The JSON API include examples](./../general/adapters.md#JSON-API)
124+
## Including Nested Resources
112125

113-
With the above modifications, you can execute code as below in order to include nested resources while doing a find query.
126+
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)
114127

115128
```javascript
116-
store.findRecord('post', postId, { adapterOptions: { query: { include: 'comments' } } });
129+
store.findRecord('post', postId, { include: 'comments' } );
117130
```
118-
will generate the path `/posts/{postId}?include='comments'`
131+
which will generate the path /posts/{postId}?include='comments'
119132

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

0 commit comments

Comments
 (0)