@@ -3,22 +3,50 @@ layout: guides
3
3
---
4
4
# Getting started - Ruby on Rails
5
5
6
- Start by adding the following to your `Gemfile`:
6
+ Start by adding the `jsonapi-rails` gem to your `Gemfile`:
7
7
8
8
```ruby
9
9
gem 'jsonapi-rails'
10
10
```
11
11
12
- Then, once your [serializable resources](/guides/serialization) are defined, building
13
- a JSON API document from your business objects is straightforward: simply pass them
14
- to the `render` method.
12
+ Then run `bundle install`.
15
13
16
- For a comprehensive list of renderer options, see
17
- [Renderer options](/guides/serialization/rendering.html).
14
+ ## Defining Serializable and Deserializable resources
18
15
19
- ## Rendering resources
16
+ Now define your serializable and deserializable resources.
17
+
18
+ ### Serializable resources
20
19
21
20
```ruby
21
+ # app/serializers/post.rb
22
+ class SerializablePost < JSONAPI::Serializable::Resource
23
+ type 'posts'
24
+
25
+ attributes :title, :content, :tag_ids, :created_at, :updated_at
26
+
27
+ belongs_to :author
28
+ has_many :comments
29
+ has_many :users
30
+ end
31
+ ```
32
+
33
+ ### Deserializable resources
34
+
35
+ ```ruby
36
+ # app/deserializers/post.rb
37
+ class DeserializablePost < JSONAPI::Deserializable::Resource
38
+ attributes :title, :content, :tag_ids
39
+ end
40
+ ```
41
+
42
+ ## Rendering resources from controllers
43
+
44
+ Once your [serializable resources]( /guides/serialization) are defined, building
45
+ a JSON API document from your business objects is straightforward. `jsonapi-rails` ties in
46
+ to the standard Rails [`render`](https://guides.rubyonrails.org/layouts_and_rendering.html) method.
47
+
48
+ ```ruby
49
+ # app/controllers/posts_controller.rb
22
50
class PostsController < ActionController::Base
23
51
# ...
24
52
@@ -32,7 +60,10 @@ class PostsController < ActionController::Base
32
60
end
33
61
```
34
62
35
- ## Rendering relationships
63
+ For a comprehensive list of renderer options, see
64
+ [Renderer options](/guides/serialization/rendering.html).
65
+
66
+ ### Rendering relationships
36
67
37
68
```ruby
38
69
class PostsController < ActionController::Base
@@ -50,7 +81,7 @@ class PostsController < ActionController::Base
50
81
end
51
82
```
52
83
53
- ## Rendering errors
84
+ ### Rendering errors
54
85
55
86
```ruby
56
87
class PostsController < ActionController::Base
@@ -70,6 +101,73 @@ class PostsController < ActionController::Base
70
101
end
71
102
```
72
103
104
+ ### A full controller example
105
+ ```ruby
106
+ # app/controllers/posts_controller.rb
107
+ class PostsController < ActionController::Base
108
+ deserializable_resource :post, only: [:create, :update]
109
+
110
+ def index
111
+ render jsonapi: Posts.all,
112
+ include: [:author, comments: [:author]],
113
+ fields: { users: [:name, :email],
114
+ posts: [:title, :content] }
115
+ end
116
+
117
+ def show
118
+ render jsonapi: post, rendering_options
119
+ end
120
+
121
+ def create
122
+ post = Post.create(create_params)
123
+
124
+ if post.save
125
+ render jsonapi: post, rendering_options.merge(status: :created)
126
+ else
127
+ render jsonapi_errors: post.errors
128
+ end
129
+ end
130
+
131
+ def update
132
+ post = Post.find(params[:id])
133
+ post.assign_attributes(update_params)
134
+
135
+ if post.save
136
+ render jsonapi: post, rendering_options
137
+ else
138
+ render jsonapi_errors: post.errors
139
+ end
140
+ end
141
+
142
+ def destroy
143
+ post = Post.find(params[:id])
144
+
145
+ if post.destroy
146
+ head :no_content
147
+ else
148
+ head :conflict
149
+ end
150
+ end
151
+
152
+ private
153
+
154
+ def create_params
155
+ params.require(:post).permit(:title, :content, :tag_ids)
156
+ end
157
+
158
+ def update_params
159
+ params.require(:post).permit(:title, :content, :tag_ids)
160
+ end
161
+
162
+ def rendering_options
163
+ {
164
+ include: [:author, comments: [:author]],
165
+ fields: { users: [:name, :email] }
166
+ }
167
+ end
168
+ end
169
+ ```
170
+
73
171
## Serializable Class Lookup
74
172
75
173
By default, for an instance of `Post`, the corresponding serializable resource class
0 commit comments