【旧版】Rails:学習:1.3.最初のアプリケーション



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 アプリを作成する
cd ~/environment
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$ bundle install

[!] There was an error parsing `Gemfile`: Illformed requirement ["9,0.6"]. Bundl

er cannot continue.

 #  from /Users/user/environment/hello_app/Gemfile:14
 #  -------------------------------------------
 #    gem 'sqlite3', '1.3.13'
 >    gem 'byebug', '9,0.6', platform: :mri
 #  end
 #  -------------------------------------------

パンチミスしていた "9,0.6" → "9.0.6"

もう一度実行

(base) users-iMac:hello_app user$ bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler

 is installing for. Bundler is installing for ruby but the dependency is only fo

r x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bun

dle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Could not find gem 'jquery-rais (= 4.3.1)' in any of the gem sources listed in
your Gemfile.

→ 今度はOKかな?
これでプログラムは完成とのこと。
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
Could not find gem 'puma (= 3.9.1)' in any of the gem sources listed in your Gem

file.
Run `bundle install` to install missing gems.

→ pumaが無いって言っている?
pumaはweサーバーっぽい。

gem listで確認
(base) users-iMac:hello_app user$ gem list

*** LOCAL GEMS ***

actioncable (5.2.2, 5.1.6.1, 5.1.6)
 :
puma (3.12.0)
 :
puma
ありました。でも3.12.0?
いったん削除する。
gem uninstall puma
(base) users-iMac:hello_app user$ gem uninstall puma
Remove executables:
        puma, pumactl

in addition to the gem? [Yn]  y
Removing puma
Removing pumactl
Successfully uninstalled puma-3.12.0

3.9.1をインストール
gem install puma -v 3.9.1
(base) users-iMac:hello_app user$ gem install puma -v 3.9.1
Fetching puma-3.9.1.gem
Building native extensions. This could take a while...
Successfully installed puma-3.9.1
Parsing documentation for puma-3.9.1
Installing ri documentation for puma-3.9.1
Done installing documentation for puma after 12 seconds
1 gem installed

念のため gem list で再確認。
(base) users-iMac:hello_app user$ gem list
 :
puma (3.9.1)
 :

あらためて rails server
(base) users-iMac:hello_app user$ rails server
Could not find gem 'sass-rails (= 5.0.6)' in any of the gem sources listed in yo

ur Gemfile.
Run `bundle install` to install missing gems.

→ 同じ原因っぽい。
※ sass-rails = CSS生成言語?

5.0.7が入っていたのでアンインストール→5.0.6インストール
(base) users-iMac:hello_app user$ gem uninstall sass-rails
Successfully uninstalled sass-rails-5.0.7
(base) users-iMac:hello_app user$ gem install sass-rails -v 5.0.6
Fetching sass-rails-5.0.6.gem
Successfully installed sass-rails-5.0.6
Parsing documentation for sass-rails-5.0.6
Installing ri documentation for sass-rails-5.0.6
Done installing documentation for sass-rails after 0 seconds
1 gem installed

gem server
(base) users-iMac:hello_app user$ rails server
Could not find gem 'uglifier (= 3.2.0)' in any of the gem sources listed in your

 Gemfile.
Run `bundle install` to install missing gems.
※ uglifier = UglifyJS2というJavaScriptのRuby版?

4.1.20が入っていたのでアンインストール→3.2.0インストール
(base) users-iMac:hello_app user$ gem uninstall uglifier
Successfully uninstalled uglifier-4.1.20
(base) users-iMac:hello_app user$ gem install uglifier -v 3.2.0
Fetching uglifier-3.2.0.gem
Successfully installed uglifier-3.2.0
Parsing documentation for uglifier-3.2.0
Installing ri documentation for uglifier-3.2.0
Done installing documentation for uglifier after 2 seconds
1 gem installed

gem server
(base) users-iMac:hello_app user$ gem server
Server started at http://[::]:8808
Server started at http://0.0.0.0:8808
→ いけた?
ブラウザで http://192.168.56.101:8808 を開いてみる。

なんか違うような…

→ environmentフォルダを削除し始めからやり直してみる。
→ bundle install をやってみたが…
(base) users-iMac:hello_app user$ bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler

 is installing for. Bundler is installing for ruby but the dependency is only fo

r x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bun

dle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
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.

→ …


[Ruby]
https://railstutorial.jp/chapters/beginning?version=5.1#sec-installing_rails

0 件のコメント:

その他の記事