[Ruby on Rails 3, Ruby on Rails 4] Rails4 をインストールして新規プロジェクトを始める
すぐに gem install rails したいところですが、Bundler でプロジェクト毎に rails のバージョンを管理するようなので試してみます。
[markdown]
参考サイト
> * [Rails開発環境の構築(rbenvでRuby導入からBundler、Rails導入まで) – Qiita [キータ]](http://qiita.com/emadurandal/items/a60886152a4c99ce1017)
> * [Ruby + bundler + Rails => Hello World – Qiita [キータ]](http://qiita.com/iwot@github/items/d1487adf334e536bed15)
ちなみに同じ手順で Rails3 が入ることも確認しました。
## Bundler を利用して Rails プロジェクトの雛形を作成する
プロジェクトディレクトリを作成して、bundle init します。
“`
% mkdir lessons-rails4
% cd lessons-rails4
% bundle init
Writing new Gemfile to /Users/***/projects/lessons-rails4/Gemfile
% ls
Gemfile
“`
生成された Gemfile を編集して、rails の最新版を設定する。
“`
% vim Gemfile
“`
“`ruby:Gemfile
# A sample Gemfile
source “https://rubygems.org”
gem “rails”
“`
これで最新版が指定されます。
通常は、バージョンを指定することになると思います。
### Gemfile のバージョン指定方法
“`
gem ‘rails’ # 最新バージョン
gem ‘rails’, ‘3.1.12’ # 3.1.12
gem ‘rails’, ‘>= 3.1.12’ # 3.1.12 以上
gem ‘rails’, ‘~> 3.1.12’ # 3.1.12 以上、3.2 未満
“`
gemは、このように探すとはかどりそうです。
“`
% gem query –remote –all –name-matches “^rails$”
“`
> * [Snow Leopard の Rails が消えていたけど、おかげで gem search | list | query の違いがわかった – 彼女からは、おいちゃんと呼ばれています](http://d.hatena.ne.jp/inouetakuya/20120129/1327838190)
> * [bundlerでgemをプロジェクトごとに管理する | Developers.IO](http://dev.classmethod.jp/server-side/language/bundler-gem-management/)
> * [RubyGems User Guide | RubyGems Manuals](http://docs.rubygems.org/read/chapter/16)
railsを vender/bundle 以下にインストールする。
“`
% bundle install –path vendor/bundle
Fetching gem metadata from https://rubygems.org/………..
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies…
Installing rake (10.1.0)
Installing i18n (0.6.4)
Installing minitest (4.7.5)
Installing multi_json (1.7.7)
Installing atomic (1.1.10)
Installing thread_safe (0.1.0)
Installing tzinfo (0.3.37)
Installing activesupport (4.0.0)
Installing builder (3.1.4)
Installing erubis (2.7.0)
Installing rack (1.5.2)
Installing rack-test (0.6.2)
Installing actionpack (4.0.0)
Installing mime-types (1.23)
Installing polyglot (0.3.3)
Installing treetop (1.4.14)
Installing mail (2.5.4)
Installing actionmailer (4.0.0)
Installing activemodel (4.0.0)
Installing activerecord-deprecated_finders (1.0.3)
Installing arel (4.0.0)
Installing activerecord (4.0.0)
Using bundler (1.3.5)
Installing hike (1.2.3)
Installing thor (0.18.1)
Installing railties (4.0.0)
Installing tilt (1.4.1)
Installing sprockets (2.10.0)
Installing sprockets-rails (2.0.0)
Installing rails (4.0.0)
Your bundle is complete!
It was installed into ./vendor/bundle
“`
次回以降は、–path vendor/bundle ディレクトリを指定しなくてもそのディレクトリが使用されるとのこと。
また、この設定は.bundle/configの中に出力されています。
> * [Bundler再履修: bundle execって何? gemはどこに入るの? – memo.yomukaku.net](http://memo.yomukaku.net/entries/IpCSQmo)
“`
% ls -a
./ .bundle/ Gemfile.lock
../ Gemfile vendor/
% bundle config
Settings are listed in order of priority. The top value will be used.
path
Set for your local app (/Users/***/projects/lessons-rails4/.bundle/config): “vendor/bundle”
disable_shared_gems
Set for your local app (/Users/***/projects/lessons-rails4/.bundle/config): “1”
“`
## Rails プロジェクトのディレクトリを作成する
bundle exec rails new で雛形を上書きし、rails プロジェクトのディレクトリを作成します。
雛形作成に利用したGemfileファイルを上書きします。
skip-bundle を忘れないこと。
“`
% bundle exec rails new . –skip-bundle
exist
create README.rdoc
create Rakefile
create config.ru
create .gitignore
conflict Gemfile
Overwrite /Users/***/projects/lessons-rails4/Gemfile? (enter “h” for help) [Ynaqdh] y
force Gemfile
create app
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/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
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/filter_parameter_logging.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 lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
“`
> * [Ruby環境のgemをuninstall – Decimal Blue Note](http://fukuishere.github.io/blog/2013/03/23/gem-unistall/)
> * [RubyOnRails-Bundler CapmNetwork](http://capm-network.com/?tag=RubyOnRails-Bundler)
下記のファイルが生成されました。
また、上書きされた Gemfile の内容が書き換わっています。
“`
% ls -a
./ Gemfile app/ db/ test/
../ Gemfile.lock bin/ lib/ tmp/
.bundle/ README.rdoc config/ log/ vendor/
.gitignore Rakefile config.ru public/
“`
## プロジェクトの環境設定
Gemlock.lock とvendor/bundle を git の管理から外します。
“`
% vim .gitignore
“`
“`:.gitignore
/*.lock
/vendor/bundle
“`
その他のプロジェクトに必要な gem をGemfile に追記し、あらためて bundle install をします。
“`
% bundle install –path vendor/bundle
“`
> * [Rails – Bundlerを使ったRubyGemsの依存関係管理 – Qiita [キータ]](http://qiita.com/sumyapp/items/6843ceebbbfc09a2b6ac)
> * [橋本商会 » Ruby書くならBundler使え](http://shokai.org/blog/archives/7262)
## サーバを起動する
以下のコマンドでサーバを起動します。
“`
% bundle exec rails server
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-07-01 00:13:28] INFO WEBrick 1.3.1
[2013-07-01 00:13:28] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.4.0]
[2013-07-01 00:13:28] INFO WEBrick::HTTPServer#start: pid=13444 port=3000
Started GET “/” for 127.0.0.1 at 2013-07-01 00:14:10 +0900
Processing by Rails::WelcomeController#index as HTML
Rendered vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb (2.7ms)
Completed 200 OK in 97ms (Views: 34.5ms | ActiveRecord: 0.0ms)
“`
サーバには http://localhost:3000/ でアクセスできます。
About your application’s environment からも、作成したプロジェクトが使われていることが分かります。
Ctrl + C
でサーバを停止します。
“`
^C[2013-07-01 00:28:41] INFO going to shutdown …
[2013-07-01 00:28:41] INFO WEBrick::HTTPServer#start done.
Exiting
“`
## Rails 用のWebサーバ
Rails 用のWebサーバについては、完熟トマトさんで触れられていました。
> * [Mac OS X 10.7.5 (LION)でRuby on Railsの簡易アプリを作成してみる – 完熟トマト](http://kanjuku-tomato.blogspot.jp/2013/01/mac-os-x-1075-lionruby-on-railsweb.html)
とりあえず WEBrick で動かしてみますが、実際の開発・運用はどんな形で行われているのか、追々調べる必要はありそうです。
## 補遺
rake の上書きをしてもよいか確認される場合もあるよう。
“`
Updating installed gems
Updating rake
Fetching: rake-10.1.0.gem (100%)
rake’s executable “rake” conflicts with /Users/***/.rbenv/versions/2.0.0-p247/bin/rake
Overwrite the executable? [yN] y
Successfully installed rake-10.1.0
Parsing documentation for rake-10.1.0
Installing ri documentation for rake-10.1.0
Installing darkfish documentation for rake-10.1.0
Gems updated: rake
“`
conflict に関してQAがありました。
> * [ruby2.0.0-p0でgem update するとrakeのconflict が起きる – QA@IT](http://qa.atmarkit.co.jp/q/3018)
[/markdown]