アプリケーションの土台作成

このセクションでは、ブログアプリケーションの実行に必要なリソースと、これから開発するプログラムを格納するフォルダ構造を備えたアプリケーションの土台を作成する方法を解説します。

 

<チュートリアルを進めるにあたって>

  •  ・アプリケーション名は"blog"、アプリケーションのルートフォルダは、"c:\rails_apps\blog"とします。

  •  ・アプリケーションのルートフォルダのパスは、"RAILS_ROOT"と記載します。

 

コマンドプロンプトの起動とフォルダの移動

アプリケーションを作成するフォルダに移動します。コマンドプロンプトで「cd c:\rails_apps」とタイプしてEnterキーを押下してください。

C:\Users\ruby>cd c:\rails_apps
C:\rails_apps>

 

 

Generatorを用いたアプリケーションの土台作成

「Generator」と呼ばれるRailsの自動生成機能を用いて、アプリケーションの土台となるファイル、フォルダを作成します。コマンドプロンプトで「rails new blog --skip-bundle」とタイプしてEnterキーを押下します。コマンドプロンプトに、ファイル・フォルダが自動生成される様子が以下のように表示されます。

C:\rails_apps>rails new blog --skip-bundle
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/images/rails.png
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/mailers
create app/models
create app/views/layouts/application.html.erb
create app/mailers/.gitkeep
create app/models/.gitkeep
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/secret_token.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create doc
create doc/README_FOR_APP
create lib
create lib/tasks
create lib/tasks/.gitkeep
create lib/assets
create lib/assets/.gitkeep
create log
create log/.gitkeep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/index.html
create public/robots.txt
create script
create script/rails
create test/fixtures
create test/fixtures/.gitkeep
create test/functional
create test/functional/.gitkeep
create test/integration
create test/integration/.gitkeep
create test/unit
create test/unit/.gitkeep
create test/performance/browsing_test.rb
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.gitkeep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.gitkeep
create vendor/plugins
create vendor/plugins/.gitkeep

 

参考1:オプション「--skip-bundle」

アプリケーションの土台作成時に、Railsがデフォルトで必要とするgemパッケージのインストールをスキップするためのオプションです。

開発するアプリケーションの数が多くなり、それぞれのアプリケーションで異なるgemパッケージが必要になると、gemパッケージの管理が煩雑になります。アプリケーションの土台作成時には、gemパッケージのインストールを行わずに、後段で説明する「bundle install」を用いて、アプリケーション毎に必要なgemパッケージを導入することをお勧めします。

--skip-bundleオプションを指定しない場合は、Ruby処理系のインストール先にgemパッケージがインストールされます。

 

参考2:オプション「--database データベースシステム名」

アプリケーションが利用するデータベースシステムを指定するためのオプションです。何も指定しない場合は「SQLite」が選択されますので、別のデータベースシステムを使用する場合は、「--database データベースシステム名」の形式で指定する必要があります。    

例1)MySQLを使用する場合
rails new blog --skip-bundle --database mysql

 

例2)PostgreSQLを使用する場合
rails new blog --skip-bundle --database postgresql

 

参考3:データベース接続の定義ファイル

データベース接続の定義情報は、"RAILS_ROOT\config"フォルダの下にある"database.yml"ファイルに記述されています。本チュートリアルでは、SQLiteを使用しますので、SQLiteへ接続するための定義情報が自動的に登録されています。

 

アプリケーションのルートフォルダ(RAILS_ROOT)への移動

コマンドプロンプトで「cd  blog」とタイプしてEnterキーを押下してください。

c:\rails_apps>cd blog
c:\rails_apps\blog>

 

アプリケーションの実行に必要なgemパッケージのインストール

SQLiteを用いたデータベースにアクセスするためのライブラリなど、アプリケーションの実行に必要なgemパッケージをインストールする必要がありますので、「bundle install --path vendor\bundle」とタイプしてEnterキーを押下してください。すると、"RAILS_ROOT\vendor\bundle"フォルダの配下に、アプリケーションの実行に必要なgemパッケージがインストールされます。

c:\rails_apps\blog>bundle install --path vendor\bundle
Fetching source index for https://rubygems.org/
Installing rake (0.9.2.2)
Installing i18n (0.6.0)
Installing multi_json (1.0.4)
Installing activesupport (3.2.1)
Installing builder (3.0.0)
Installing activemodel (3.2.1)
Installing erubis (2.7.0)
Installing journey (1.0.1)
Installing rack (1.4.1)
Installing rack-cache (1.1)
Installing rack-test (0.6.1)
Installing hike (1.2.1)
Installing tilt (1.3.3)
Installing sprockets (2.1.2)
Installing actionpack (3.2.1)
Installing mime-types (1.17.2)
Installing polyglot (0.3.3)
Installing treetop (1.4.10)
Installing mail (2.4.1)
Installing actionmailer (3.2.1)
Installing arel (3.0.0)
Installing tzinfo (0.3.31)
Installing activerecord (3.2.1)
Installing activeresource (3.2.1)
Using bundler (1.0.21)
Installing coffee-script-source (1.2.0)
Installing execjs (1.3.0)
Installing coffee-script (2.2.0)
Installing rack-ssl (1.3.2)
Installing json (1.6.5) with native extensions
Installing rdoc (3.12)
Installing thor (0.14.6)
Installing railties (3.2.1)
Installing coffee-rails (3.2.2)
Installing jquery-rails (2.0.0)
Installing rails (3.2.1)
Installing sass (3.1.15)
Installing sass-rails (3.2.4)
Installing sass-rails (3.2.4)
Installing sqlite3 (1.3.5)
Installing uglifier (1.2.3)
Your bundle is complete! It was installed into ./vendor/bundle

 

参考1:「--path フォルダ名」オプション

指定されたフォルダにgemパッケージをインストールします。オプションを指定しない場合は、Ruby処理系のインストール環境にgemパッケージをインストールします。

 

アプリケーションの起動・停止」に続きます。