Skip to content

Commit 8224b2a

Browse files
authored
Merge pull request #29 from patch-malone/update-rails-getting-started-guide
Update the rails getting started guide
2 parents 7c858ec + 996316f commit 8224b2a

File tree

4 files changed

+112
-9
lines changed

4 files changed

+112
-9
lines changed

source/guides/getting_started/rails.html.md.erb

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,50 @@ layout: guides
33
---
44
# Getting started - Ruby on Rails
55

6-
Start by adding the following to your `Gemfile`:
6+
Start by adding the `jsonapi-rails` gem to your `Gemfile`:
77

88
```ruby
99
gem 'jsonapi-rails'
1010
```
1111

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`.
1513

16-
For a comprehensive list of renderer options, see
17-
[Renderer options](/guides/serialization/rendering.html).
14+
## Defining Serializable and Deserializable resources
1815

19-
## Rendering resources
16+
Now define your serializable and deserializable resources.
17+
18+
### Serializable resources
2019

2120
```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
2250
class PostsController < ActionController::Base
2351
# ...
2452

@@ -32,7 +60,10 @@ class PostsController < ActionController::Base
3260
end
3361
```
3462

35-
## Rendering relationships
63+
For a comprehensive list of renderer options, see
64+
[Renderer options](/guides/serialization/rendering.html).
65+
66+
### Rendering relationships
3667

3768
```ruby
3869
class PostsController < ActionController::Base
@@ -50,7 +81,7 @@ class PostsController < ActionController::Base
5081
end
5182
```
5283

53-
## Rendering errors
84+
### Rendering errors
5485

5586
```ruby
5687
class PostsController < ActionController::Base
@@ -70,6 +101,73 @@ class PostsController < ActionController::Base
70101
end
71102
```
72103

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+
73171
## Serializable Class Lookup
74172

75173
By default, for an instance of `Post`, the corresponding serializable resource class

source/peeks/_hanami.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ module API::Controllers::Posts
3030
end
3131
end
3232
```
33+
Further instructions are in the [getting started guide for Hanami](guides/getting_started/hanami).

source/peeks/_plain.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ resource.to_h
3030
# comments_types: ['comments', 'comments', 'comments']
3131
# }
3232
```
33+
34+
Further instructions are in the [getting started guide for plain Ruby](guides/getting_started/plain_ruby).

source/peeks/_rails.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ class PostsController < ApplicationController
1919
end
2020
end
2121
```
22+
23+
Further instructions are in the [getting started guide for Rails](guides/getting_started/rails).

0 commit comments

Comments
 (0)