3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

31日目 Ruby on Rails を起動したい その3

Last updated at Posted at 2019-04-10

さてさて、Railsのインストール、続きます。

昨日のふりかえり

30日目 Ruby on Rails を起動したい その2

前日シャットダウンしたパソコンを起動して、さて pg 1.1.4 をインストールしようと思ったら動かない!

なぜか古い方のrubyが起動されていました。前日の作業前の状態です。。。がっかりしていたらrbenv global 2.5.0するとよいですよとコメント頂きました。ありがたや!しかしそれでも動かず。

よくみたらパスにRubyが入っておらず、前日のログをみたら.bash_profileにRubyのエントリーを入れたのにrails sが起動しない原因ととりちがえて外していたのでした。そこで、これを戻してsource ~/.bash_profileからのrbenv global 2.5.0で起動しました!ふう。

gem install pg -v '1.1.4' を実行すると、今度はlibpq-fe.hがないとエラー!
このファイルを探しまくったのですが、みつからず・・・。
他に方法がないか検索したら、なんでもいいので、postgresをインストールしてしまえば良いようです。

brew install postgresql からの gem install pg -v '1.1.4'でOK!

そしてrails sしたところpuma-3.12.1がないよとエラーになったのでした。


本日はここからっ!

rails sのエラーメッセージを確認

$ rails s
Could not find puma-3.12.1 in any of the sources
Run `bundle install` to install missing gems.

puma-3.12.1ってなんだろう?

モダンなWebサーバー、というものらしい。
並列処理が得意、らしい。
一番最初にQUICK STARTのご案内があって親切だなあと思いました。
A MODERN, CONCURRENT WEB SERVER FOR RUBY
A MODERN, CONCURRENT WEB SERVER FOR RUBY

さっそくやってみましょう。
QUICKSTART WITH BUNDLER

$ gem "puma"
ERROR:  While executing gem ... (Gem::CommandLineError)
    Unknown command puma

あら。bundle使えない?なんでかな。
ならばQUICKSTART WITHOUT BUNDLERです。

$ gem install puma
Fetching: puma-3.12.1.gem (100%)
Building native extensions. This could take a while...
Successfully installed puma-3.12.1
Parsing documentation for puma-3.12.1
Installing ri documentation for puma-3.12.1
Done installing documentation for puma after 1 seconds
1 gem installed

インストールできたのかな?これでいいのかな?

$ rails s
Could not find rubyzip-1.2.2 in any of the sources
Run `bundle install` to install missing gems.

rubyzip-1.2.2が見つからないって?

うへえ。またか!!!

(所要時間 1時間)


コメント頂きました。

ありがとうございます!

bundle install すれば必要な gem が一気にインストールされるので,一つ一つ

$ gem install puma
などとする必要はありません。

「Gemfile にこう書いとけ」という指示

なるほど。Gemfileを見てみます。

$ cat Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'

(以下略)

ははあ・・・なるほど。
動かすのに必要な道具をセットにしているわけですね。
bundle install は最寄りのGemfileを見ながら、書いている通りににインストールしていくわけだ。
pumaもpqもある。あら??? rubyzipは見当たらない。

$ grep rubyzip Gemfile
$

もやっとしますが、ものは試しです。バックアップをとって。

bundle install してみましょう。(3回目!)

$ bundle install
Fetching gem metadata from https://rubygems.org/............

(中略)

Fetching rubyzip 1.2.2
Installing rubyzip 1.2.2 ←ありますね!

(中略)

Bundle complete! 20 Gemfile dependencies, 85 gems now installed. ← complete!
Use `bundle info [gemname]` to see where a bundled gem is installed.
Post-install message from sass:

Ruby Sass has reached end-of-life and should no longer be used.

* If you use Sass as a command-line tool, we recommend using Dart Sass, the new
  primary implementation: https://sass-lang.com/install

* If you use Sass as a plug-in for a Ruby web framework, we recommend using the
  sassc gem: https://github.com/sass/sassc-ruby#readme

* For more details, please refer to the Sass blog:
  https://sass-lang.com/blog/posts/7828841

$ 

Bundle complete! きました!!!
最後に Sass は使えませんと書いてるような。気になりますが・・・

rails s やってみましょう!

$ rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/" for ::1 at 2019-04-10 14:04:50 +0900
  
ActiveRecord::PendingMigrationError (

Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=development

):

起動したした・・・Errかい!!!

Err.png

次回 ActiveRecord::PendingMigrationError の謎。

Railsの道は遠い・・・

(所要時間 1時間)


rails db:migrate statusでステータスを見る。

エラーメッセージで検索したら、似たようなケースをみつけました。

「ActiveRecord::PendingMigrationError」と出た時のエラー解決方法【rails】

なるほど。rails db:migrate statusで見てみます。

$ rails db:migrate status
== 20190407044449 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0038s

(中略)
rails aborted!
Don't know how to build task 'status' (See the list of available tasks with `rake --tasks`)
Did you mean?  stats

(以下略)

'status' というタスクの作り方がわかんないよ!って事かな???
rake --tasks で使えるタスクを確認しましょう、でいいのかな?

$ rake --tasks
rake about                              # List versions of all Rails framew...
(以下略)
### たくさん出てきたので

$ rake --tasks | grep status
rake db:migrate:status                  # Display status of migrations

statusあります。いいはずなんだけど。

ひとつ戻って、エラーメッセージを見ると

ActiveRecord::PendingMigrationError (

Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=development

):

だから、bin/rails db:migrate RAILS_ENV=developmentを実行すればいいのかな???わからないことだらけです!

bin/rails db:migrate RAILS_ENV=development

rake db:migrate、railsでDBを作ったら、最初にmigrateするものらしい。
RAILS_ENVは開発環境か本番環境かを指定するものらしい。

やってみましょう。

$ bin/rails db:migrate RAILS_ENV=development
$ 

特になんのメッセージもないけど・・・これでいいのかな???

rails s しましょう(7回目)

$ rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

なんだかいい感じ!
http://localhost:3000/ を開くと・・・

OK.png

起動しました!!!

(所要時間 1時間)

29日目 Railsのインストール
29日目 その2 Ruby on Railsを起動したい!
30日目 Ruby on Rails を起動したい その2
31日目 Ruby on Rails を起動したい その3
(トータル 12時間くらい?)

3
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?