Rails 5の環境構築(続き)
前回はRails 5のインストールまでを行いました。
今回はRailsプロジェクトの作成とデータベース作成、pumaサーバーの起動を行います。
Railsプロジェクトの作成
Railsプロジェクトを作成します。
RailsのデフォルトのデータベースはSQLiteですが、本連載ではデータベースにPostgreSQLを使用するため、-d postgresオプションを指定します。
$ bundle exec rails new . -d postgresql
exist
create README.md
create Rakefile
create config.ru
create .gitignore
conflict Gemfile
Overwrite /Users/chikuba/sandbox/rails/rails5_sample/Gemfile? (enter "h" for help) [Ynaqdh]⇒ (「Y」と入力)
force Gemfile
create app
create app/assets/config/manifest.js
…(中略)…
Installing sass-rails 5.0.6
Bundle complete! 15 Gemfile dependencies, 63 gems now installed.
Bundled gems are installed into ./vendor/bundle.
run bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
rails newコマンドではGemfileを生成しますが、既にbundle initによって生成されたGemfileが存在しているため、上書きするか聞いてきます。ここでは上書きするため、Yを入力します。
このように、一通りRailsで必要となるファイル群が生成された後、デフォルトでは自動でbundle installが実行されます。これはGemfileがrails newコマンドによって上書きされ、追加で定義されたgemパッケージのインストールが必要なためです。bundle installを手動で行いたい場合は--skip-bundleオプションを指定します。
実際にrails newコマンドで再生成されたGemfileの中身は、以下の通りです。
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0', '>= 5.0.0.1' # Use postgresql as the database for Active Record gem 'pg', '~> 0.18' # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' …(中略)… # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
-d postgresqlオプションを指定したので、Gemfileの通り、データベースアダプターのgemがpgとなっています。このオプションがないとデフォルトではgem sqlite3となります。
データベース接続の設定
Railsアプリケーションが使用するデータベースの設定を確認します。データベースの設定ファイルはconfig/databases.ymlです。
default: &default
adapter: postgresql
encoding: unicode
…中略…
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: rails5_sample_development
…中略…
test:
<<: *default
database: rails5_sample_test
…中略…
production:
<<: *default
database: rails5_sample_production
username: rails5_sample
password: <%= ENV['RAILS5_SAMPLE_DATABASE_PASSWORD'] %>
このように、デフォルトでは開発環境用(development)、テスト環境用(test)、本番環境用(production)のデータベース名の設定が入っています。データベースのホストはlocalhost、ポート番号は5432が、それぞれデフォルト値です。
データベース名はプロジェクト名_環境と、環境のポストフィックスが付いていることが分かります。
config/databases.ymlのencodingがデフォルトでunicodeになっていますので、これをutf8に変更します。
default: &default adapter: postgresql encoding: utf8 …中略…
なお、ローカルPCへのPostgreSQLインストール時、特にオプション等を指定していなければログインユーザーでパスワードなしのデータベース作成権限が付与されているはずなので、config/databases.ymlのusername、passwordは指定なしでも問題ありません。
ひとまずは開発環境/テスト環境用のデータベースをRails経由で作成しましょう。以下のコマンドを実行します。
$ bundle exec rails db:create Created database 'rails5_sample_development' Created database 'rails5_sample_test'
以前はrakeコマンドで実行していましたが、Rails 5ではrailsコマンドでデータベースを作成することができるようになりました。また、以前はデータベース作成されたメッセージが出力されませんでしたが、メッセージが出力されるようになっています。
Railsアプリケーションの起動
いよいよRailsアプリケーションの起動です。
rails sコマンドでRailsアプリケーションを起動してみましょう。
$ bundle exec rails s => Booting Puma => Rails 5.0.0.1 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://localhost:3000 Use Ctrl-C to stop
無事にWebサーバーのPumaが起動しました。
前回触れたように、RailsのデフォルトのWebサーバーがWEBrickからPumaに変更されています。
Webサーバーを起動したのでWebブラウザで「http://localhost:3000」にアクセスします。以下のような画面が出力されれば無事、Rails 5の開発環境が整いました。
Railsアプリケーションを起動したコンソールに戻ると、以下のようなログが出力されていることが確認できます。
Started GET "/" for ::1 at 2016-09-04 02:36:08 +0900
Processing by Rails::WelcomeController#index as HTML
Parameters: {"internal"=>true}
Rendering vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/templates/rails/welcome/index.html.erb
Rendered vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/templates/rails/welcome/index.html.erb (5.2ms)
Completed 200 OK in 30ms (Views: 14.3ms | ActiveRecord: 0.0ms)
