ほぼゼロから Hello, Rails!

# 注意
本スライドは **[オズビジョンもくもく会(第3回)](https://ja-jp.facebook.com/events/274477339589936/)** で
もくもくやった内容をQiita Slideに書き留め
イキオイでそのままLTに使ったものです。

----

# 自己紹介

- @terra_yucco (オズビジョン3年目Eng)
- 2008-2013: C, Bash, VBA
- 2014: Bash, AWS Cli, Nagios
- 2015-: Bash, PHP, GAS

----

# 前提のおはなし

enPiTプログラム参加中。(事前講義Done)
PBLプログラムはRuby on Railsのため、
Hello Rails! を動かすべく **もくもく** します

----

# 事前の状態
- vagrantとvirtualboxインストール済
- rubyはインストール済
- よく見たらrailsもいた

```
vagrant@enpit:~$ which rails
/home/vagrant/.rbenv/shims/rails
```

----

# 参考

http://ruby-rails.hatenadiary.com/entry/20140813/1407915718

----

# 事前に必要なもの

![image](https://qiita-image-store.s3.amazonaws.com/0/95030/4027d0e2-0044-3363-28b5-8c011d60fa60.png)

----

# 全部用意されてた

```
vagrant@enpit:~$ which ruby
/home/vagrant/.rbenv/shims/ruby
vagrant@enpit:~$ which rails
/home/vagrant/.rbenv/shims/rails
vagrant@enpit:~$ which sqlite3
/usr/bin/sqlite3
```

----

# なのでこの辺からやるよ！
![image](https://qiita-image-store.s3.amazonaws.com/0/95030/9d470211-43ab-7bd6-21b2-b9e3f6ea57e7.png)

----

# 最初はrails new
rubyって開始処理がほんと親切

```
vagrant@enpit:~$ pwd
/home/vagrant
vagrant@enpit:~$ rails new ~/ozmoku
      create
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  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  bin/setup
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.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
         run  bundle install
Fetching gem metadata from https://rubygems.org/
Fetching version metadata from https://rubygems.org/
Fetching dependency metadata from https://rubygems.org/
Resolving dependencies.......................................................................................
Using rake 11.2.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.9.0
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.1.0
Using pkg-config 1.1.7
Using rack 1.6.4
Using mime-types-data 3.2016.0521
Using arel 6.0.3
Using debug_inspector 0.0.2
Using bundler 1.12.5
Using byebug 9.0.5
Using coffee-script-source 1.10.0
Using execjs 2.7.0
Using thor 0.19.1
Using concurrent-ruby 1.0.2
Using multi_json 1.12.1
Using sass 3.4.22
Using tilt 2.0.5
Using spring 1.7.2
Using sqlite3 1.3.11
Using turbolinks-source 5.0.0
Using rdoc 4.2.2
Using tzinfo 1.2.2
Using nokogiri 1.6.8
Using rack-test 0.6.3
Using mime-types 3.1
Using binding_of_caller 0.7.2
Using coffee-script 2.4.1
Using uglifier 3.0.1
Using sprockets 3.7.0
Using turbolinks 5.0.1
Using sdoc 0.4.1
Using activesupport 4.2.6
Using loofah 2.0.3
Using mail 2.6.4
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.7
Using activemodel 4.2.6
Using jbuilder 2.6.0
Using rails-html-sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using activejob 4.2.6
Using activerecord 4.2.6
Using actionview 4.2.6
Using actionpack 4.2.6
Using actionmailer 4.2.6
Using railties 4.2.6
Using sprockets-rails 3.1.1
Using coffee-rails 4.1.1
Using jquery-rails 4.1.1
Using rails 4.2.6
Using sass-rails 5.0.6
Using web-console 2.3.0
Bundle complete! 12 Gemfile dependencies, 57 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
vagrant@enpit:~$
```

----

# rails server 起動しない-1
- usageでる
- http://pyoonn.hatenablog.com/entry/2014/10/07/234237
    - 先人は偉大
- cdすりゃいいらしい

----

# rails server 起動しない-2
- Gem Load Error is: Could not find a JavaScript runtime.
- http://pyoonn.hatenablog.com/entry/2014/10/17/165416
    - 先人は偉大

- fix

```
echo "gem 'therubyracer'" >> ~/ozmoku/Gemfile
bundle install
rails s
```

----

# Mission Complete !!

```
vagrant@enpit:~/ozmoku$ rails s
=> Booting WEBrick
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-08-09 11:03:14] INFO  WEBrick 1.3.1
[2016-08-09 11:03:14] INFO  ruby 2.2.5 (2016-04-26) [i686-linux]
[2016-08-09 11:03:14] INFO  WEBrick::HTTPServer#start: pid=14299 port=3000
```

----

# Misssion Incomplete...

![image](https://qiita-image-store.s3.amazonaws.com/0/95030/56edc94b-37a4-1125-1413-c9d28d4068cd.png)

----

# localhost:3000 接続できない-1

- http://qiita.com/tmf16/items/bb789a45c0f456eac11f
- http://qiita.com/arakaji/items/216bcbb77d7a8127310d
- http://zacodesign.net/blog/?p=2022
    - 先人は偉大

----

# localhost:3000 接続できない-2

- 他にもそもそもvmにnetwork設定無かったので以下を付けた
    - config.vm.network "private_network", ip: "192.168.33.10"
    - config.vm.network "public_network"
- Ctrl + C して、rails s -b 0.0.0.0

----

# Mission Complete !!

![image](https://qiita-image-store.s3.amazonaws.com/0/95030/76667707-d6e9-e645-ac39-5c3b8813b715.png)

----

# Hello Rails を表示する

- 呪文: rails generate controller welcome index
    - 訳: welcomeコントローラ(indexアクション風味)を作るよ！

----

```
vagrant@enpit:~/ozmoku$ rails generate controller welcome index
Running via Spring preloader in process 2380
      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/welcome.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.scss
vagrant@enpit:~/ozmoku$
```

----

# あとやること

- `echo '<h1>Hello, Rails!</h1>' > app/views/welcome/index.html.erb`
- config/routes.rb「# root 'welcome#index'」を「root 'welcome#index'」に変更

----

# Quest Finish !!

![image](https://qiita-image-store.s3.amazonaws.com/0/95030/b7547d18-2742-c6fb-78c1-27e7d3ca128c.png)

----

# 本来は
http://ruby-rails.hatenadiary.com/entry/20140813/1407915718
元々の資料に従うとブログっぽいものができるのですが今日はここまで！

----

# まとめ

----

# 先人は偉大

----

# Ruby on Rails 初期構築が超楽