LoginSignup
0
3

More than 3 years have passed since last update.

rails new からrails serverまでの流れ

Last updated at Posted at 2020-02-06

この記事はrails newしたい時に色々忘れていたりするので自分用のメモです
都度更新していくかもしれません。

rails newまで

gemはvendor/bundleで管理したい

  • フォルダ作成からAtomへ移動まで
~
❯ cd MyApp

~/MyApp
❯ mkdir portfolio

~/MyApp
❯ cd portfolio

~/MyApp/portfolio
❯ bundle init
Writing new Gemfile to /Users/kn428/MyApp/portfolio/Gemfile

~/MyApp/portfolio
❯ atom .
  • rails5.2の環境にしたかったので、Gemfileのgem "rails"のコメントアウトを外し、gem 'rails', '~> 5.2.1'とする
  • bundler経由でrailsをインストールする
~/MyApp/portfolio
❯ bundle install --path vendor/bundle --jobs=4
  • bundle exec経由でrails newにopを付けて実行する
    .は現在のディレクトリの意味
    opはrails new -hで確認またはググる
~/MyApp/portfolio 59s
❯ bundle exec rails new . -B -d mysql --skip-test
       exist
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /Users/kn428/MyApp/portfolio/Gemfile? (enter "h" for help) [Ynaqdhm] Y

Gemfileを上書きしていいか聞かれたらYで続行

参考 :
新規Railsプロジェクトの作成手順まとめ
rails new 手順書

Gemfileに必要なGemを追加

  • gemfileに下記をコピペする(※私の場合)
    汎用性のためSLIM等は外しておく
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.4'

gem 'rails', '~> 5.2.1'
gem 'bootsnap', require: false
gem 'mysql2', '~> 0.5.2'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails', '~> 4.3'
gem 'turbolinks',   '~> 5.0'
gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'

group :development, :test do
  gem 'sqlite3', '~> 1.3.6'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'spring-commands-rspec'
  gem 'pry-rails'
  gem 'pry-doc'
  gem 'pry-byebug'
  gem 'rails-erd'
  gem 'annotate'
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'rubocop-airbnb'
  gem 'bullet'
end

group :test do
  gem 'capybara'
  gem 'webdrivers'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

これで$ bundle updateしたところ下記のエラーが発生

An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

様々な対処法があるらしいが私は下記で対処できた
1. $ brew info opensslを実行し
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
この箇所の""内の部分をコピーしてメモ帳などで貼っておく(環境によるのでちゃんとコマンドを実行して確認すること)
2. それを下記のコマンドに繋げる
--with-cppflags
--with-ldflags
3. 最後にbundle configコマンドに繋げる
$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl@1.1/include"
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib"
すると.bundle/config内に追加されている(--with-cppflagsのほうが上書きされている気がするが気にしないでおく)

.bundle/config
---
BUNDLE_PATH: "vendor/bundle"
BUNDLE_JOBS: "4"
BUNDLE_BUILD__MYSQL2: "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib"

これで$ bundle updateしたら無事全てインストールできた

参考 :
mysql2 gemインストール時のトラブルシュート
Bundlerでビルドオプションを指定する

installしたGemの設定

Rspec関連を設定する

$ bundle exec rails generate rspec:install
.rspecのフォルダの中に--format documentationを追加する
config/application.rb内を下記の状態に変更する

.config/application.rb
module Portfolio
  class Application < Rails::Application
  # ...省略...
    config.time_zone = 'Tokyo'
    config.generators do |g|
      g.test_framework :rspec,
                       view_specs: false,
                       helper_specs: false,
                       controller_specs: false,
                       routing_specs: false,
                       request_specs: false
    end
    config.generators.system_tests   = false
    config.generators.stylesheets    = false
    config.generators.javascripts    = false
    config.generators.helper         = false
  end
end

spec/rails_helper.rbに以下を追記

RSpec.configure do |config|
  # ...省略...
  config.include FactoryBot::Syntax::Methods # 追加
end

テスト用DBをマイグレーションしておく
$ bundle exec rails db:migrate RAILS_ENV=test

  • Capybaraの初期設定
    $ mkdir spec/supports
    $ touch spec/supports/capybara.rb
    spec/supports/capybara.rb内に下記を実装
RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end
end

spec/spec_helper.rbに下記を追加
require 'supports/capybara'

  • SystemとRequest Specの追加
    $ mkdir spec/system
    $ mkdir spec/requests

  • Rspecの高速化
    $ bundle exec spring binstub --all
    $ bin/rspec spec/のコマンドで動けばok。direnvの設定は次回の機会にまわす

参考 :
RSpecを導入してテストを書いてみる
spring と direnv を使って Rails と rspec を高速起動。快適開発はじめる

rails server

$ bundle exec rails db:create
$ bundle exec rails db:migrate
$ bundle exec rails s
無事(http://localhost:3000/) に「Yay! You’re on Rails!」が表示されれば完了

git init ~ push

使いまわせたら嬉しいのでここまでをgithub等にpushしておく
まず.gitignoreの中に/vendor/bundleを入れておく
$ echo '/vendor/bundle' >> .gitignore

$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add origin https://github.com/ユーザー名/リポジトリ名.git
$ git push -u origin master

以上です。何かアドバイス等ありましたらコメントいただけると嬉しいです。

0
3
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
3