0
1

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 3 years have passed since last update.

備忘録: Rails APIアプリ作成時のチートシート

Last updated at Posted at 2020-06-04

app作成コマンド

rails バージョン new アプリ名 -d mysql --webpacker=(vue or react) --skip-turbolinks

追加するgem list

gem 'devise'
gem 'devise-i18n'
gem 'rails-i18n'

gem 'faker'
gem 'factory_bot_rails'

gem 'rubocop', require: false
gem 'rubocop-rails', require: false
gem 'pry'
gem 'pry-doc'
gem 'pry-rails'
gem 'pry-byebug'
gem 'foreman'

gem 'rspec-rails', '~> 4.0.0'
gem 'rubocop-rspec', require: false

実行コマンド

$ rails webpacker:install

$ rails webpacker:install:vue

$ yarn add axios

$ yarn add vue-router

$ rails g rspec:install

$ rails g devise:install

rubocopの設定

..rubocop.yml
require: rubocop-rails

AllCops:
  # Rubyのバージョンを指定
  TargetRubyVersion: 2.6.5
  # 除外するファイル
  Exclude:
    - vendor/bundle/**/*
    - bin/*
    - db/**/*
    - config/**/*
    - Gemfile
    - node_modules/**/*

# Missing magic comment # frozen_string_literal: true. を無視
Style/FrozenStringLiteralComment:
  Enabled: false

# Missing top-level class documentation comment. を無視
Style/Documentation:
  Enabled: false

# Line is too long を無視
Metrics/LineLength:
  Enabled: false

# Use nested module/class definitions instead of compact style. を無視
Style/ClassAndModuleChildren:
  Enabled: false

rspecの設定

.rspec
--require spec_helper
--format documentation

foremanの設定

Profile.dev
web: bundle exec rails s
# watcher: ./bin/webpack-watcher
hot: ./bin/webpack-dev-server
bin/server
# /bin/bash -i
bundle install --path vendor/bundle
bundle exec foreman start -f Procfile.dev
$ chmod 777 bin/server

日本語対応といらないファイルを作成しない設定

config/application.rb
config.i18n.default_locale = :ja
config.time_zone = "Tokyo"

config.generators do |g|
  g.template_engine false 
  g.assets false
  g.helper false
  g.test_framework :rspec,
                    fixtures: false,
                    view_specs: false,
                    helper_specs: false,
                    routing_specs: false
end

よく使う作成コマンド

$ rails g devise:install
$ rails g devise user
$ rails g model モデル名 title:string likable:references{polymorphic}

# rails generate migration クラス名 カラム名:データ型( カラム名:データ型)
$ rails g migration AddDetailsToTitles price:integer author:string
$ rails g migration RemoveAuthorFromTitles author:string

$ rails g controller api/v1/コントローラー名 index create

$ rails g factory_bot:model モデル名

$ rails g rspec:request api/v1/portfolios/post

PULL_REQUEST_TEMPLATE

## 前提プルリク

先にレビュー/マージする必要があるPRがある場合は記載。
なければ「なし」と記載。

## Issue番号

関連するIssueがある場合は記載。
なければ「なし」と記載。

## 概要

目的、変更内容など。必須。
タイトルで十分に伝わる場合は「表題の通り」と記入。

## 参考記事など

ない場合は省略可。

## 注意点

ない場合は省略可。

## スクリーンショット

画面がない場合は省略可。

## チェックリスト
- [ ] rubocopが警告やエラーを出さないこと
- [ ] rspecが全てパスしていること

Can't verify CSRF token authenticity(都度調べてるエラー)

上記エラーに関する参考URL: https://7me.nobiki.com/2018/07/30/rails-vue-axios-csrf-token/

XXS(クロスサイトスクリプティング)とCSRF(クロスサイトリクエストフォージェリ)の違い
参考URL: https://qiita.com/wanko5296/items/142b5b82485b0196a2da

const token = document.getElementsByName("csrf-token")[0].getAttribute("content");
axios.defaults.headers.common["X-CSRF-Token"] = token;
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?