Hello Worldプログラムを作ってみる
■environmentディレクトリ作成
練習プログラムはホームディレクトリ配下のenvironmentディレクトリに作成することとする。
ということでディレクトリを作成し移動
cd
mkdir environment
cd environment/
(base) users-iMac:~ user$ cd
(base) users-iMac:~ user$ mkdir environment
(base) users-iMac:~ user$ cd environment/
(base) users-iMac:environment user$
■プロジェクト作成
rails 5.1.6 で hello_app アプリを作成する
rails _5.1.6_ new hello_app
(base) users-iMac:environment user$ cd ~/environment
(base) users-iMac:environment user$ rails _5.1.6_ new hello_app
create
create README.md
:
* For more details, please refer to the Sass blog:
http://sass.logdown.com/posts/7081811
run bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
■Gemfile置換
~/environment/hello_app/Gemfileファイルを
参考サイトの内容で置換する。


これは、チュートリアルがうまく動くようにバージョン指定を行っているらしい。
以下内容を張り付ける。
source 'https://rubygems.org'
gem 'rails', '5.1.6'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.6.4'
group :development, :test do
gem 'sqlite3', '1.3.13'
gem 'byebug', '9.0.6', platform: :mri
end
group :development do
gem 'web-console', '3.5.1'
gem 'listen', '3.1.5'
gem 'spring', '2.0.2'
gem 'spring-watcher-listen', '2.0.1'
end
# Windows環境ではtzinfo-dataというgemを含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
修正したらgemをインストール
※ gem … ライブラリのことらしい
※ bundle … gemの管理プログラムのことらしい
cd ~/environment/hello_app/
bundle install
(base) users-iMac:environment user$ cd ~/environment/hello_app
(base) users-iMac:hello_app user$ bundler install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler
:
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (= 5.1.6.1)
In Gemfile:
rails (= 5.1.6) was resolved to 5.1.6, which depends on
activesupport (= 5.1.6)
coffee-rails (= 4.2.2) was resolved to 4.2.2, which depends on
railties (>= 4.0.0) was resolved to 5.1.6.1, which depends on
activesupport (= 5.1.6.1)
Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
→ エラーとなった。
activesupport 5.1.6.1 が必要?
■activesupportインストール
gem install activesupport -v 5.1.6.1
(base) users-iMac:hello_app user$ gem install activesupport -v 5.1.6.1
Successfully installed activesupport-5.1.6.1
Parsing documentation for activesupport-5.1.6.1
Installing ri documentation for activesupport-5.1.6.1
Done installing documentation for activesupport after 4 seconds
1 gem installed
もう一度 bundler install を実行
→ 同じエラーとなった
■activesupportインストール
gem uninstall activesupport で削除してみる。
5.1.6が入っていたので5.1.6のみ削除
(base) users-iMac:hello_app user$ gem uninstall activesupport
Select gem to uninstall:
1. activesupport-5.1.6
2. activesupport-5.1.6.1
3. All versions
> 1
You have requested to uninstall the gem:
activesupport-5.1.6
actionpack-5.1.6 depends on activesupport (= 5.1.6)
actionview-5.1.6 depends on activesupport (= 5.1.6)
activejob-5.1.6 depends on activesupport (= 5.1.6)
activemodel-5.1.6 depends on activesupport (= 5.1.6)
activerecord-5.1.6 depends on activesupport (= 5.1.6)
rails-5.1.6 depends on activesupport (= 5.1.6)
railties-5.1.6 depends on activesupport (= 5.1.6)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN] y
Successfully uninstalled activesupport-5.1.6
もう一度 bunder install を実行
→ 同じエラーとなった。エラーメッセージをよく見ると
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (= 5.1.6.1)
もしかして Gemfile.lock というファイルを消せばよい?
~/environment/hello_app/GEnfile.lock を削除
もう一度 bundler install を実行
今度はうまくいったようだ。
:
Using turbolinks-source 5.2.0
Fetching turbolinks 5.0.1
Installing turbolinks 5.0.1
Fetching uglifier 3.2.0
Installing uglifier 3.2.0
Fetching web-console 3.5.1
Installing web-console 3.5.1
Bundle complete! 15 Gemfile dependencies, 64 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
これでプログラムは完成。
rainsには簡易webサーバ機能があり結果を確認できる。
パスがhello_appにあることを確認して以下コマンドを実行
rails server
(base) users-iMac:hello_app user$ pwd
/Users/user/environment/hello_app
(base) users-iMac:hello_app user$ rails server
=> Booting Puma
=> Rails 5.1.6 application starting in development
:
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
ブラウザで http://192.168.56.101:3000 を開いてみる。
→ OK
[Ruby]
https://railstutorial.jp/chapters/beginning?version=5.1#sec-installing_rails