@@ -8,8 +8,37 @@ Simple yet powerful way to get your Rails API compliant with [JSON API](http://j
88
99JSONAPI::Utils (JU) is built on top of [ JSONAPI::Resources] ( https://github.com/cerebris/jsonapi-resources ) taking advantage of its resource-driven style and bringing a Rails way to build modern APIs with no or less learning curve.
1010
11+ * [ Installation] ( #installation )
12+ * [ How does it work?] ( #how-does-it-work )
13+ * [ Usage] ( #usage )
14+ * [ Response] ( #response )
15+ * [ Rendes] ( #renders )
16+ * [ Formatters] ( #formatters )
17+ * [ Request] ( #request )
18+ * [ Params helpers] ( #params-helpers )
19+ * [ Full example] ( #full-example )
20+ * [ Models] ( #models )
21+ * [ Resources] ( #resources )
22+ * [ Routes & Controllers] ( routes--controllers )
23+ * [ Initializer] ( #initializer )
24+ * [ Requests & Responses] ( requests--responses )
25+ * [ Index] ( #index )
26+ * [ Index (options)] ( #index-options )
27+ * [ Show] ( #show )
28+ * [ Show (options)] ( #show-options )
29+ * [ Relationships (identifier objects)] ( #relationships-identifier-objects )
30+ * [ Nested resources] ( #nested-resources )
31+ * [ Development] ( #development )
32+ * [ Contributing] ( #contributing )
33+ * [ License] ( #license )
34+
1135## Installation
1236
37+ For:
38+
39+ * Ruby ~ > 2.0 with Rails ~ > 4.0, use JU's ` 0.4.6 ` version (stable);
40+ * Ruby ~ > 2.1 with Rails 5, use JU's ` 0.5.0.beta1 ` version.
41+
1342Add these lines to your application's Gemfile:
1443
1544``` ruby
@@ -248,11 +277,12 @@ Rails.application.routes.draw do
248277end
249278```
250279
251- In our base controller we need to include the ` JSONAPI::Utils ` module and define some default rendering:
280+ In our Rails controller we just need to include the ` JSONAPI::Utils ` module.
281+ Note that with JU some default rendering can be defined like ` jsonapi_render_not_found ` for when ActiveRecord doesn't find a record:
252282
253283``` ruby
254284# app/controllers/base_controller.rb
255- class BaseController < JSONAPI :: ResourceController
285+ class BaseController < ActionController :: Base
256286 include JSONAPI ::Utils
257287 protect_from_forgery with: :null_session
258288 rescue_from ActiveRecord ::RecordNotFound , with: :jsonapi_render_not_found
@@ -263,43 +293,43 @@ Finally, having inhirited `JSONAPI::Utils` methods from the `BaseController` we
263293
264294``` ruby
265295# app/controllers/users_controller.rb
266- # GET /users
267- def index
268- users = User .all
269- jsonapi_render json: users
270- end
296+ # GET /users
297+ def index
298+ users = User .all
299+ jsonapi_render json: users
300+ end
271301
272- # GET /users/:id
273- def show
274- user = User .find(params[:id ])
275- jsonapi_render json: user
276- end
302+ # GET /users/:id
303+ def show
304+ user = User .find(params[:id ])
305+ jsonapi_render json: user
306+ end
277307
278- # POST /users
279- def create
280- user = User .new (resource_params)
281- if user.save
282- jsonapi_render json: user, status: :created
283- else
284- jsonapi_render_errors json: user, status: :unprocessable_entity
285- end
308+ # POST /users
309+ def create
310+ user = User .new (resource_params)
311+ if user.save
312+ jsonapi_render json: user, status: :created
313+ else
314+ jsonapi_render_errors json: user, status: :unprocessable_entity
286315 end
316+ end
287317
288- # PATCH /users/:id
289- def update
290- user = User .find(params[:id ])
291- if user.update(resource_params)
292- jsonapi_render json: user
293- else
294- jsonapi_render_errors json: user, status: :unprocessable_entity
295- end
296- end
297-
298- # DELETE /users/:id
299- def destroy
300- User .find(params[:id ]).destroy
301- head :no_content
318+ # PATCH /users/:id
319+ def update
320+ user = User .find(params[:id ])
321+ if user.update(resource_params)
322+ jsonapi_render json: user
323+ else
324+ jsonapi_render_errors json: user, status: :unprocessable_entity
302325 end
326+ end
327+
328+ # DELETE /users/:id
329+ def destroy
330+ User .find(params[:id ]).destroy
331+ head :no_content
332+ end
303333```
304334
305335And:
@@ -350,8 +380,6 @@ JSONAPI.configure do |config|
350380 config.json_key_format = :underscored_key
351381 config.route_format = :dasherized_route
352382
353- config.operations_processor = :active_record
354-
355383 config.allow_include = true
356384 config.allow_sort = true
357385 config.allow_filter = true
@@ -389,7 +417,7 @@ Here's some examples of requests – based on those sample [controllers](#routes
389417* [ Relationships (identifier objects)] ( #relationships-identifier-objects )
390418* [ Nested resources] ( #nested-resources )
391419
392- #### Collection
420+ #### Index
393421
394422Request:
395423
@@ -459,7 +487,7 @@ Content-Type: application/vnd.api+json
459487}
460488```
461489
462- #### Collection (options)
490+ #### Index (options)
463491
464492Request:
465493
@@ -544,7 +572,7 @@ Content-Type: application/vnd.api+json
544572}
545573```
546574
547- #### Single record
575+ #### Show
548576
549577Request:
550578
@@ -584,7 +612,7 @@ Content-Type: application/vnd.api+json
584612}
585613```
586614
587- #### Single record (options)
615+ #### Show (options)
588616
589617Request:
590618
0 commit comments