読者です 読者をやめる 読者になる 読者になる

rails newの先輩オススメ手順

railsプロジェクトを新しく作成する際に実行する「rails new」のオススメ手順を、会社の先輩に教えていただいたのでメモ。

前提

  • Rubyインストール済み
  • アプリケーション名:rails-app-name

手順

railsアプリケーション用のディレクトリを作成する。
→作成したディレクトリに移動

$ mkdir rails-app-name
$
$ cd rails-app-name
$
$ pwd
/Users/maetoo11/workspace/rails-app-name
$
$ ls -l
$

railsアプリケーション用ディレクトリでbundle initを実行し、Gemfileを作成する。

$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$
$ bundle init
Writing new Gemfile to /Users/maetoo11/workspace/rails-app-name/Gemfile
$
$
$
$ ls -l
total 8
-rw-r--r--  1 maetoo11  staff  64  3  3 17:37 Gemfile

Gemfileを編集してrailsのgem を追加する。

$ vi Gemfile
  1 source 'https://rubygems.org'
  2
  3 gem 'rails'

bundle installでGemfileで指定されたgemのインストールを行う。

ここで大事なこと!

$ bundle install --path vendor/bundle --jobs=4
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using json 1.8.3
Installing i18n 0.7.0
Installing minitest 5.8.4
Installing rake 10.5.0
Installing builder 3.2.2
・・・略・・・
Installing rails 4.2.5.2
Bundle complete! 1 Gemfile dependency, 34 gems now installed.
Bundled gems are installed into ./vendor/bundle.
$
$ ls -l
total 16
-rw-r--r--  1 maetoo11  staff    63  3  3 17:37 Gemfile
-rw-r--r--  1 maetoo11  staff  2607  3  3 17:40 Gemfile.lock
drwxr-xr-x  3 maetoo11  staff   102  3  3 17:40 vendor
$
$ # vendor/bundle配下にgemがインストールされていることを確認
$ ls -la vendor/bundle/ruby/2.3.0/gems
total 0
drwxr-xr-x  34 maetoo11  staff  1156  3  3 17:40 .
drwxr-xr-x   9 maetoo11  staff   306  3  3 17:40 ..
drwxr-xr-x   6 maetoo11  staff   204  3  3 17:40 actionmailer-4.2.5.2
drwxr-xr-x   6 maetoo11  staff   204  3  3 17:40 actionpack-4.2.5.2
・・・略・・・

rails newを実行する際に、付与するオプションを決める。
→まずはヘルプを表示して、オプションを確認

$ bundle exec rails new -h
Usage:
  rails new APP_PATH [options]
Options:
  -r, [--ruby=PATH]                                      # Path to the Ruby binary of your choice
                                                         # Default: /Users/maetoo11/.rbenv/versions/2.3.0/bin/ruby
  -m, [--template=TEMPLATE]                              # Path to some application template (can be a filesystem path or URL)
      [--skip-gemfile], [--no-skip-gemfile]              # Don't create a Gemfile
  -B, [--skip-bundle], [--no-skip-bundle]                # Don't run bundle install
  -G, [--skip-git], [--no-skip-git]                      # Skip .gitignore file
      [--skip-keeps], [--no-skip-keeps]                  # Skip source control .keep files
  -O, [--skip-active-record], [--no-skip-active-record]  # Skip Active Record files
  -S, [--skip-sprockets], [--no-skip-sprockets]          # Skip Sprockets files
      [--skip-spring], [--no-skip-spring]                # Don't install Spring application preloader
  -d, [--database=DATABASE]                              # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
                                                         # Default: sqlite3
  -j, [--javascript=JAVASCRIPT]                          # Preconfigure for selected JavaScript library
                                                         # Default: jquery
  -J, [--skip-javascript], [--no-skip-javascript]        # Skip JavaScript files
      [--dev], [--no-dev]                                # Setup the application with Gemfile pointing to your Rails checkout
      [--edge], [--no-edge]                              # Setup the application with Gemfile pointing to Rails repository
      [--skip-turbolinks], [--no-skip-turbolinks]        # Skip turbolinks gem
  -T, [--skip-test-unit], [--no-skip-test-unit]          # Skip Test::Unit files
      [--rc=RC]                                          # Path to file containing extra configuration options for rails command
      [--no-rc], [--no-no-rc]                            # Skip loading of extra configuration options from .railsrc file
Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist
Rails options:
  -h, [--help], [--no-help]        # Show this help message and quit
  -v, [--version], [--no-version]  # Show Rails version number and quit
Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.
    You can specify extra command-line arguments to be used every time
    'rails new' runs in the .railsrc configuration file in your home directory.
    Note that the arguments specified in the .railsrc file don't affect the
    defaults values shown above in this help message.
Example:
    rails new ~/Code/Ruby/weblog
    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.

今回は

  • bundle installを飛ばす
  • DBにMySQLを指定
  • turbolinksを使用しない
  • test-unitを使用しない

を指定してrails newを実行する。

2016/12/16 追記
Rails5からは
bundle exec rails new -B -d mysql --skip-turbolinks --skip-test.
らしいです。(未確認です)

$ bundle exec rails new -B -d mysql --skip-turbolinks --skip-test-unit .
       exist
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /Users/maetoo11/workspace/rails-app-name/Gemfile? (enter "h" for help) [Ynaqdh]
       force  Gemfile
      create  app
・・・略・・・
$
$ ls -l
total 40
-rw-r--r--   1 maetoo11  staff  1392  3  3 17:48 Gemfile
-rw-r--r--   1 maetoo11  staff  2607  3  3 17:40 Gemfile.lock
-rw-r--r--   1 maetoo11  staff   478  3  3 17:48 README.rdoc
-rw-r--r--   1 maetoo11  staff   249  3  3 17:48 Rakefile
drwxr-xr-x   8 maetoo11  staff   272  3  3 17:48 app
drwxr-xr-x   6 maetoo11  staff   204  3  3 17:48 bin
drwxr-xr-x  11 maetoo11  staff   374  3  3 17:48 config
-rw-r--r--   1 maetoo11  staff   153  3  3 17:48 config.ru
drwxr-xr-x   3 maetoo11  staff   102  3  3 17:48 db
drwxr-xr-x   4 maetoo11  staff   136  3  3 17:48 lib
drwxr-xr-x   3 maetoo11  staff   102  3  3 17:48 log
drwxr-xr-x   7 maetoo11  staff   238  3  3 17:48 public
drwxr-xr-x   3 maetoo11  staff   102  3  3 17:48 tmp
drwxr-xr-x   4 maetoo11  staff   136  3  3 17:48 vendor

Gemfileを編集し、不要なgemの削除と必要なgemの追加を行う。
→今回は開発・テスト環境でwebrickではなくthinを使うようにした。(39行目)

$ vi Gemfile
  1 source 'https://rubygems.org'
  2
  3
  4 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
  5 gem 'rails', '4.2.5.2'
  6 # Use mysql as the database for Active Record
  7 gem 'mysql2', '>= 0.3.13', '< 0.5'
 ・・・略・・・
 35
 36 group :development do
 37   # Access an IRB console on exception pages or by using <%= console %> in views
 38   gem 'web-console', '~> 2.0'
 39   gem 'thin'
 40
 41   # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
 42   gem 'spring'
 43 end

vendor/bundle配下をgitで管理しないファイルにするために、.gitignoreを編集する。(9行目)

$ vi .gitignore
  1 # See https://help.github.com/articles/ignoring-files for more about ignoring files.
  2 #
  3 # If you find yourself ignoring temporary files generated by your text editor
  4 # or operating system, you probably want to add a global ignore instead:
  5 #   git config --global core.excludesfile '~/.gitignore_global'
  6
  7 # Ignore bundler config.
  8 /.bundle
  9 /vendor/bundle
 10
 11 # Ignore all logfiles and tempfiles.
 12 /log/*
 13 !/log/.keep
 14 /tmp

bundlerの設定を確認する。
→最初のbundle install時に指定したオプションが設定されている。
→以降、このプロジェクトディレクトリで実行するbundlerはこの設定を使用する。

$ cat .bundle/config
---
BUNDLE_PATH: vendor/bundle
BUNDLE_JOBS: 4
BUNDLE_DISABLE_SHARED_GEMS: '1'

bundle installを実行する。

$ bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.5.0
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.4
・・・略・・・
Bundle complete! 11 Gemfile dependencies, 53 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

これでrails newのオススメ手順は終わりです。