37
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails | Rails API

Posted at

Rails | Rails API

概要

Rails::APIはRailsでWeb-APIのみを提供するアプリケーションを作成するためのgemです。

railsコマンドと同等のコマンドが多数用意されていますが、
各種 generate 系コマンド実行時に views が生成されません。
代わりに各アクションメソッドはJSONを返却します。

前提

rails-apiをインストールしておきます

$ gem i rails-api

サンプル

仕様

下記のモデルを扱います

Person
┠name : string
┗age : integer

手順

  • 新規プロジェクトを作成します
$ rails-api new api_sample
  • 出力された構成

views がないことを確認できます

$ cd api_sample
$ tree -d
|-- app
|   |-- assets
|   |   `-- images
|   |-- controllers
|   |   `-- concerns
|   |-- mailers
|   `-- models
|       `-- concerns
|-- bin
|-- config
|   |-- environments
|   |-- initializers
|   `-- locales
|-- db
|-- lib
|   |-- assets
|   `-- tasks
|-- log
|-- public
`-- test
    |-- controllers
    |-- fixtures
    |-- helpers
    |-- integration
    |-- mailers
    `-- models
  • scaffoldします
$ rails-api g scaffold person name:string age:integer
      invoke  active_record
      create    db/migrate/20140626233217_create_people.rb
      create    app/models/person.rb
      invoke    test_unit
      create      test/models/person_test.rb
      create      test/fixtures/people.yml
      invoke  api_resource_route
       route    resources :people, except: [:new, :edit]
      invoke  scaffold_controller
      create    app/controllers/people_controller.rb
      invoke    test_unit
      create      test/controllers/people_controller_test.rb
$ rake db:migrate
  • テスト用のデータを挿入します
sqlite> insert into people("name", "age", "created_at", "updated_at") values ('tanaka', 20, datetime('now'), datetime('now'));
sqlite> insert into people("name", "age", "created_at", "updated_at") values ('suzuki', 19, datetime('now'), datetime('now'));
sqlite> select * from people;
1|tanaka|20|2014-06-26 23:38:37|2014-06-26 23:38:37
2|suzuki|19|2014-06-26 23:38:37|2014-06-26 23:38:37
  • Strong Parametersの設定を追加します

PeopleControllerに下記の private method を追加

private

def person_params
  params.require(:person).permit(:name, :age)
end

PeopleControllerの create メソッドの new の引数を person_params メソッドに変更する

def create
  # 修正前
  # @person = Person.new(params[:person])
  # 修正後
  @person = Person.new(person_params)

  if @person.save
    render json: @person, status: :created, location: @person
  else
    render json: @person.errors, status: :unprocessable_entity
  end
end
  • Webサーバーを起動します
$ rails s
  • Web-APIを呼び出します。 index を呼び出して全 person を取得します
$ curl http://xxxxxxxx:3000/people
[{"id":1,"name":"tanaka","age":20,"created_at":"2014-06-26T23:38:37.000Z","updated_at":"2014-06-26T23:38:37.000Z"},{"id":2,"name":"suzuki","age":19,"created_at":"2014-06-26T23:38:37.000Z","updated_at":"2014-06-26T23:38:37.000Z"}]
  • Web-APIを呼び出します。 show を呼び出して任意の idperson を取得します
$ curl http://xxxxxxxx:3000/people/1
{"id":1,"name":"tanaka","age":20,"created_at":"2014-06-26T23:38:37.000Z","updated_at":"2014-06-26T23:38:37.000Z"}
  • Web-APIを呼び出します。 create を呼び出して新規 person を作成します。
$ curl http://xxxxxxxx:3000/people -X POST -d "person[name]=sato" -d "person[age]=21"
{"id":3,"name":"sato","age":21,"created_at":"2014-06-26T23:51:06.129Z","updated_at":"2014-06-26T23:51:06.129Z"}
  • データを確認
sqlite> select * from people;
1|tanaka|20|2014-06-26 23:38:37|2014-06-26 23:38:37
2|suzuki|19|2014-06-26 23:38:37|2014-06-26 23:38:37
3|sato|21|2014-06-26 23:51:06.129997|2014-06-26 23:51:06.129997

参考

  • scaffold された PeopleController は以下のようになっています
class PeopleController < ApplicationController
  # GET /people
  # GET /people.json
  def index
    @people = Person.all

    render json: @people
  end

  # GET /people/1
  # GET /people/1.json
  def show
    @person = Person.find(params[:id])

    render json: @person
  end

  # POST /people
  # POST /people.json
  def create
    @person = Person.new(params[:person])

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

  # PATCH/PUT /people/1
  # PATCH/PUT /people/1.json
  def update
    @person = Person.find(params[:id])

    if @person.update(params[:person])
      head :no_content
    else
      render json: @person.errors, status: :unprocessable_entity
    end
  end

  # DELETE /people/1
  # DELETE /people/1.json
  def destroy
    @person = Person.find(params[:id])
    @person.destroy

    head :no_content
  end
end

参照

37
38
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
37
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?