[RUBY] Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6

Building a bulletin board API with certification authorization in Rails 6 # 8 seed implementation

Introduction of ActiveModelSerializer

By including serializer, you can easily format the data returned by json.

Gemfile


...

+ # serializer
+ gem "active_model_serializers"
$ bundle

Editing config files and serializer

Once installed, create a post model serializer and an ActiveModelSerializer configuration file.

$ rails g serializer post
$ touch config/initializers/active_model_serializer.rb

app/serializers/post_serializer.rb


# frozen_string_literal: true

#
# post serializer
#
class PostSerializer < ActiveModel::Serializer
  attributes :id
end

config/initializers/active_model_serializer.rb


# frozen_string_literal: true

ActiveModelSerializers.config.adapter = :json

app/controllers/v1/posts_controller.rb


     def index
       posts = Post.order(created_at: :desc).limit(20)
-      render json: { posts: posts }
+      render json: posts
     end
 
     def show
-      render json: { post: @post }
+      render json: @post
     end
 
     def create
       post = Post.new(post_params)
       if post.save
-        render json: { post: post }
+        render json: post
       else
         render json: { errors: post.errors }
       end
@@ -27,7 +27,7 @@ module V1
 
     def update
       if @post.update(post_params)
-        render json: { post: @post }
+        render json: @post
       else
         render json: { errors: @post.errors }
       end
@@ -35,7 +35,7 @@ module V1
 
     def destroy
       @post.destroy
-      render json: { post: @post }
+      render json: @post
     end

Once you've done this, stop rails s and restart.

Check with curl

$ curl localhost:8080/v1/posts 
{"posts":[{"id":20},{"id":19},{"id":18},{"id":17},{"id":16},{"id":15},{"id":14},{"id":13},{"id":12},{"id":11},{"id":10},{"id":9},{"id":8},{"id":7},{"id":6},{"id":5},{"id":4},{"id":3},{"id":2},{"id":1}]}
$ curl localhost:8080/v1/posts/1
{"post":{"id":1}}

I was able to get a list of ids because only ids are used in serializer. Now let's add subject and body.

app/serializers/post_serializer.rb


 # frozen_string_literal: true

 #
 # post serializer
 #
 class PostSerializer < ActiveModel::Serializer
-  attributes :id
+  attributes :id, :subject, :body  
 end
$ curl localhost:8080/v1/posts
{"posts":[{"id":20,"subject":"Useless","body":"The bee's cuckoo. Violent blood grudge. The hidden ruins."},...
curl localhost:8080/v1/posts/1
{"post":{"id":1,"subject":"hello","body":"Captain General Police Officer. Meishibokin Katamichi. Traditional Tokugawa super ~.

It seems to be working normally. Let's also run rubocop and rspec and commit if there is no problem.

Continued

Introduce # 10 devise_token_auth to build bulletin board API with authentication authorization in Rails 6 [To the serial table of contents]

Recommended Posts

Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display
Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot introduced and post model
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post
Build a bulletin board API with authentication authorization in Rails 6 # 11 User model test and validation added
Build a bulletin board API with authentication authorization with Rails 6 # 2 Introducing git and rubocop
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
Building a bulletin board API with authentication authorization with Rails 6 Validation and test implementation of # 4 post
How to build API with GraphQL and Rails
I tried to make a group function (bulletin board) with Rails
How to use credentials.yml.enc introduced in Rails 5.2
How to build Rails 6 environment with Docker
Try to create a bulletin board in Java
One way to redirect_to with parameters in rails
[Rails] How to build an environment with Docker
[How to insert a video in haml with Rails]
How to query Array in jsonb with Rails + postgres
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Rails] Create API to download files with Active Storage [S3]
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to set up a proxy with authentication in Feign
Things to keep in mind when using Sidekiq with Rails
Introduced graph function with rails
Japaneseize using i18n with Rails
Implement LTI authentication in Rails
API creation with Rails + GraphQL
Introduced gRPC client to rails
I implemented Rails API with TDD by RSpec. part2 -user authentication-
What I was addicted to when implementing google authentication with rails
How to get boolean value with jQuery in rails simple form
How to rename a model with foreign key constraints in Rails
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
I implemented Rails API with TDD by RSpec. part3-Action implementation with authentication-
Steps to build a Ruby on Rails development environment with Vagrant
How to write the view when Vue is introduced in Rails?