# 現場Rails環境構築 ## 事前準備(出来てる人は飛ばしましょう) ### 前提条件 - rbenvが使えるようになっていること(まだの人は下記資料を参考にインストールしてください) https://hackmd.io/@SKjw2RY-RNCUNSdJfEWPig/BkoqCu7Rc ### ruby2.5.1をインストール ruby2.5.1はもうだいぶ古めのバージョンになるので下記オプションをつけないとインストール出来ません(新しいrubyのバージョンはつけなくてもインストールできます) ``` RUBY_CFLAGS="-Wno-error=implicit-function-declaration" rbenv install 2.5.1 ``` ### Mac上でデフォルトで使用するrubyのバージョンを2.5.1に設定 後で変えても大丈夫です ``` rbenv global 2.5.1 ``` rubyのバージョンを確認(2.5.1になっていればOK) ``` ruby -v ``` 変わらない時 ``` rbenv rehash ruby -v ``` それでも変わらない時 ``` exec $SHELL -l ruby -v ``` それでも変わらない時は再起動しましょう ### 現場Railsを進めるためのディレクトリを作成して移動する ``` mkdir -p ~/workspace/runteq/genba_rails cd ~/workspace/runteq/genba_rails ``` ## 現場Rails2章の環境構築 ### ruby2.5.1に切り替え確認(念の為) ``` ruby -v ``` 2.5.1以外になっていたら ``` rbenv global 2.5.1 ``` ### gem rails -v 5.2.6を楽にインストールする魔法のコマンドを打つ `mkdir genba_rails_install_tmp && cd genba_rails_install_tmp && bundle init && bundle add rails -v 5.2.6 && bundle install && rm Gemfile && rm Gemfile.lock && cd .. && rm -r genba_rails_install_tmp` ### rails newして作成したディレクトリに移動する 後でGemfileを編集するので`--skip-bundle`オプションをつけてbundleインストールをしないようにしています ``` rails _5.2.6_ new scaffold_app --skip-bundle cd scaffold_app ``` ### 魔法のコマンドを打つ ``` bundle add loofah -v '~> 2.19.1' ``` こんなエラーを回避出来ます ``` /Users/kenchaso/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/ loofah-2.21.1/lib/loofah/html4/document.rb:10:in `<module:HTML4>': uninitialized constant Nokogiri::HTML4 (NameError) ``` ### bundle installする 設定を追加してからbundle installしましょう ``` bundle config --local build.nokogiri --use-system-libraries bundle install ``` こんなエラーを回避できます ``` An error occurred while installing nokogiri (1.6.6.4), and Bundler cannot continue. In Gemfile: rails was resolved to 5.2.8.1, which depends on actioncable was resolved to 5.2.8.1, which depends on actionpack was resolved to 5.2.8.1, which depends on actionview was resolved to 5.2.8.1, which depends on rails-dom-testing was resolved to 2.0.3, which depends on nokogiri ``` ### Railsサーバーの起動 最後に`rails s`を実行してGoogleChromeで`http://localhost:3000`にアクセスできたら完了です ``` rails s ``` ## 現場Rails3章の環境構築(2章を行っている前提です) ### ruby2.5.1に切り替え確認(念の為) ``` ruby -v ``` 2.5.1以外になっていたら ``` rbenv global 2.5.1 ``` ### rails newして作成したディレクトリに移動する 後でGemfileを編集するので`--skip-bundle`オプションをつけてbundleインストールをしないようにしています ``` rails _5.2.6_ new taskleaf --skip-bundle cd taskleaf ``` ### 魔法のコマンドを打つ ``` bundle add loofah -v '~> 2.19.1' ``` こんなエラーを回避出来ます ``` /Users/kenchaso/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/ loofah-2.21.1/lib/loofah/html4/document.rb:10:in `<module:HTML4>': uninitialized constant Nokogiri::HTML4 (NameError) ``` ### bundle installする 設定を追加してからbundle installしましょう ``` bundle config --local build.nokogiri --use-system-libraries bundle install ``` こんなエラーを回避できます ``` An error occurred while installing nokogiri (1.6.6.4), and Bundler cannot continue. In Gemfile: rails was resolved to 5.2.8.1, which depends on actioncable was resolved to 5.2.8.1, which depends on actionpack was resolved to 5.2.8.1, which depends on actionview was resolved to 5.2.8.1, which depends on rails-dom-testing was resolved to 2.0.3, which depends on nokogiri ``` ### Railsサーバーの起動 最後に`rails s`を実行してGoogleChromeで`http://localhost:3000`にアクセスできたら完了です ``` rails s ```