Rails API

Introduction

We have summarized the steps to create an API using Rails' API mode.

procedure

  1. Create Rails app in API mode
  2. Creating a model controller
  3. Namespace-aware routing settings
  4. Controller settings
  5. Check with a browser

Create Rails app in API mode

$ rails new api-task --api

You can create an app in API mode by adding --api to the end of the normal rails new command. (By default, parts not needed for the API will not be created.)

Creating a model controller

Create a model and controller just like a regular Rails app.

rails g model category name:string
rails g model idea category_id:bigint body:text
$ rails db:create
$ rake db:migrate

Namespace-aware routing settings

By creating a namespace with the version as shown below from the beginning, future API version control will be easier.

config/routes.rb


Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v1' do
      resources :posts
    end
  end
end

Controller settings

The directory structure is as follows according to the namespace set in the root.

---- controllers

      --- api

        -- v1

         - ideas_controller.rb
         - categories_controller.rb

スクリーンショット 2020-11-10 20.29.40.png

class IdeasController < ApplicationController
  def index
    @ideas = Idea.all

     render json: @ideas
  end

  def create
     @idea = Idea.new(idea_params)

    if @idea.save
      render json: @idea, status: :created, location: @idea
    else
      render json: @idea.errors, status: :unprocessable_entity
    end
  end


  private

  def idea_params
    params.require(:idea).permit(:catagory_id,:body)
  end
end

class CategoriesController < ApplicationController
  def index
  end

  def create
    @category = Category.new(idea_params)

    if @category.save
      render json: @category, status: :created, location: @category
    else
      render json: @category.errors, status: :unprocessable_entity
    end
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end
end

Check with browser

Create some ideas and check with a browser Get(http://localhost:3000/api/v1/ideas)

スクリーンショット 2020-11-12 11.03.30.png

If it is displayed as above, it's OK! !!

Recommended Posts

Rails API
API creation with Rails + GraphQL
[Rails g.error]
Rails basics
Rails Review 1
Rails migration
Call API [Call]
[Rails] first_or_initialize
rails tutorial
About Rails 6
[Rails 6] API development using GraphQL (Query)
[Rails] Use cookies in API mode
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] devise
How to return Rails API mode to Rails
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
Try using the Rails API (zip code)
Rails API server environment construction using docker-compose
[Rails] Let's create a super simple Rails API
[Rails] Book search with Amazon PA API
Rails CSV basics
Nuxt.js × Create an application in Rails API mode
Call API [Preparation]
Rails6 jQuery introduced
Stream API memo
About Rails routing
Rails Routing Basics
Add binding.pry (rails)
Rails database basics
Rails access restrictions
Rails and FormData
rails tutorial Chapter 6
Rails tutorial test
Rails render redirect_to
[Rails] Favorite feature
Rails Bootstrap introduced
ruby API problem
[Rails] Many-to-many creation
[Rails] Naming convention
Next.js + Rails (API) + Mysql on Docker's Hello World!
Rails Credentials Note
Rails Logger Basics
[Rails] Introducing jquery
Rails6 [API mode] + MySQL5.7 environment construction with Docker
[Rails 6] Ranking function
Rails Flash Message
Tailwind on Rails
rails tutorial Chapter 1
Rails delegate method
Call API [Handling]