LoginSignup
59
47

More than 5 years have passed since last update.

Rails5のAPIモードを超速で試す

Posted at

install

gem update --system
gem install rails

Rails API アプリケーションを作る

rails new rails-api --api

--api がキモ。この指定でAPIモードになる。

APIを作る

適当にusersのAPIを作る

rails g scaffold users

Rails4の時同様、コントローラー、モデルができるがAPIモードなのでビューはできない。
コントローラーはこんな感じ。

UserController.rb
class UsersController < ApplicationController
  before_action :set_user, only: [:show, :update, :destroy]

  # GET /users
  def index
    @users = User.all

    render json: @users
  end

  # GET /users/1
  def show
    render json: @user
  end

  #・・・略
end

早速動かす

rails db:migrate

rake ではなく rails になっている。慣れていこう。

rails s

確認

curl http://localhost:3000/users

データ入れてないので空のArrayが返ってきた。

[]

まとめ

ここまでの所要時間わずか数分!!!はっや!!
とりあえず動いたのでこれから深く追っていく

以上!!

59
47
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
59
47