読者です 読者をやめる 読者になる 読者になる

Ruby on Rails チュートリアル

Ruby

 

 

railstutorial.jp

 

■環境

Cloud9

 

1.2.2 Railsをインストールする


gem install rails -v 4.2.2

1.3 最初のアプリケーション

 rails _4.2.2_ new hello_app


1.3.1 Bundler

ok

1.3.2 rails server

ok

1.3.3 Model-View-Controller (MVC)

 

1.3.4 Hello, world!

1.4 Gitによるバージョン管理

 

1.4.1 インストールとセットアップ

apori:~/workspace $ git config --global user.name "apo"
apori:~/workspace $ git config --global user.email myyahamasa@gmail.com
apori:~/workspace $ git init
Initialized empty Git repository in /home/ubuntu/workspace/.git/
apori:~/workspace (master) $ git add -A



apori:~/workspace (master) $ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: README.md
new file: hello_app/.gitignore
new file: hello_app/Gemfile
new file: hello_app/Gemfile.lock
new file: hello_app/README.rdoc
new file: hello_app/Rakefile
new file: hello_app/app/assets/images/.keep
new file: hello_app/app/assets/javascripts/application.js
new file: hello_app/app/assets/stylesheets/application.css
new file: hello_app/app/controllers/application_controller.rb
new file: hello_app/app/controllers/concerns/.keep
new file: hello_app/app/helpers/application_helper.rb
new file: hello_app/app/mailers/.keep
new file: hello_app/app/models/.keep
new file: hello_app/app/models/concerns/.keep
new file: hello_app/app/views/layouts/application.html.erb
new file: hello_app/bin/bundle
new file: hello_app/bin/rails
new file: hello_app/bin/rake
new file: hello_app/bin/setup
new file: hello_app/bin/spring
new file: hello_app/config.ru
new file: hello_app/config/application.rb
new file: hello_app/config/boot.rb
new file: hello_app/config/database.yml
new file: hello_app/config/environment.rb
new file: hello_app/config/environments/development.rb
new file: hello_app/config/environments/production.rb
new file: hello_app/config/environments/test.rb
new file: hello_app/config/initializers/assets.rb
new file: hello_app/config/initializers/backtrace_silencers.rb
new file: hello_app/config/initializers/cookies_serializer.rb
new file: hello_app/config/initializers/filter_parameter_logging.rb
new file: hello_app/config/initializers/inflections.rb
new file: hello_app/config/initializers/mime_types.rb
new file: hello_app/config/initializers/session_store.rb
new file: hello_app/config/initializers/wrap_parameters.rb
new file: hello_app/config/locales/en.yml
new file: hello_app/config/routes.rb
new file: hello_app/config/secrets.yml
new file: hello_app/db/seeds.rb
new file: hello_app/lib/assets/.keep
new file: hello_app/lib/tasks/.keep
new file: hello_app/log/.keep
new file: hello_app/public/404.html
new file: hello_app/public/422.html
new file: hello_app/public/500.html
new file: hello_app/public/favicon.ico
new file: hello_app/public/robots.txt
new file: hello_app/test/controllers/.keep
new file: hello_app/test/fixtures/.keep
new file: hello_app/test/helpers/.keep
new file: hello_app/test/integration/.keep
new file: hello_app/test/mailers/.keep
new file: hello_app/test/models/.keep
new file: hello_app/test/test_helper.rb
new file: hello_app/vendor/assets/javascripts/.keep
new file: hello_app/vendor/assets/stylesheets/.keep

ステージングエリアで控えている変更を本格的にリポジトリに反映 (コミット)

apori:~/workspace (master) $ git commit -m "Initialize repository"
[master (root-commit) 30e8e6e] Initialize repository
58 files changed, 1016 insertions(+)
create mode 100644 README.md
create mode 100644 hello_app/.gitignore
create mode 100644 hello_app/Gemfile
create mode 100644 hello_app/Gemfile.lock
create mode 100644 hello_app/README.rdoc
create mode 100644 hello_app/Rakefile
create mode 100644 hello_app/app/assets/images/.keep
create mode 100644 hello_app/app/assets/javascripts/application.js
create mode 100644 hello_app/app/assets/stylesheets/application.css
create mode 100644 hello_app/app/controllers/application_controller.rb
create mode 100644 hello_app/app/controllers/concerns/.keep
create mode 100644 hello_app/app/helpers/application_helper.rb
create mode 100644 hello_app/app/mailers/.keep
create mode 100644 hello_app/app/models/.keep
create mode 100644 hello_app/app/models/concerns/.keep
create mode 100644 hello_app/app/views/layouts/application.html.erb
create mode 100755 hello_app/bin/bundle
create mode 100755 hello_app/bin/rails
create mode 100755 hello_app/bin/rake
create mode 100755 hello_app/bin/setup
create mode 100755 hello_app/bin/spring
create mode 100644 hello_app/config.ru
create mode 100644 hello_app/config/application.rb
create mode 100644 hello_app/config/boot.rb
create mode 100644 hello_app/config/database.yml
create mode 100644 hello_app/config/environment.rb
create mode 100644 hello_app/config/environments/development.rb
create mode 100644 hello_app/config/environments/production.rb
create mode 100644 hello_app/config/environments/test.rb
create mode 100644 hello_app/config/initializers/assets.rb
create mode 100644 hello_app/config/initializers/backtrace_silencers.rb
create mode 100644 hello_app/config/initializers/cookies_serializer.rb
create mode 100644 hello_app/config/initializers/filter_parameter_logging.rb
create mode 100644 hello_app/config/initializers/inflections.rb
create mode 100644 hello_app/config/initializers/mime_types.rb
create mode 100644 hello_app/config/initializers/session_store.rb
create mode 100644 hello_app/config/initializers/wrap_parameters.rb
create mode 100644 hello_app/config/locales/en.yml
create mode 100644 hello_app/config/routes.rb
create mode 100644 hello_app/config/secrets.yml
create mode 100644 hello_app/db/seeds.rb
create mode 100644 hello_app/lib/assets/.keep
create mode 100644 hello_app/lib/tasks/.keep
create mode 100644 hello_app/log/.keep
create mode 100644 hello_app/public/404.html
create mode 100644 hello_app/public/422.html
create mode 100644 hello_app/public/500.html
create mode 100644 hello_app/public/favicon.ico
create mode 100644 hello_app/public/robots.txt
create mode 100644 hello_app/test/controllers/.keep
create mode 100644 hello_app/test/fixtures/.keep
create mode 100644 hello_app/test/helpers/.keep
create mode 100644 hello_app/test/integration/.keep
create mode 100644 hello_app/test/mailers/.keep
create mode 100644 hello_app/test/models/.keep
create mode 100644 hello_app/test/test_helper.rb
create mode 100644 hello_app/vendor/assets/javascripts/.keep
create mode 100644 hello_app/vendor/assets/stylesheets/.keep
apori:~/workspace (master) $ git log
commit 30e8e6ecc14d84c2eeddb933eaf4524189cc1a51
Author: apo <myyahamasa@gmail.com>
Date: Mon Jun 27 12:58:56 2016 +0000

Initialize repository

 

1.4.2 Gitのメリット

今の時点では、ソースコードをバージョン管理下に置かなければならない理由が今ひとつよくわからないという方がいるかもしれませんので、例を1つご紹介します。仮に、あなたが重要なapp/controllers/ディレクトリを削除してしまったとしましょう。


apori:~/workspace/hello_app (master) $ ls app/controllers/
application_controller.rb concerns/
apori:~/workspace/hello_app (master) $ rm -rf app/controllers/

apori:~/workspace/hello_app (master) $ ls app/controllers/
ls: cannot access app/controllers/: No such file or directory
apori:~/workspace/hello_app (master) $ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: app/controllers/application_controller.rb
deleted: app/controllers/concerns/.keep

no changes added to commit (use "git add" and/or "git commit -a")
apori:~/workspace (master) $
apori:~/workspace (master) $ pwd
/home/ubuntu/workspace
apori:~/workspace (master) $ ll
total 24
drwxrwxr-x 5 ubuntu ubuntu 4096 Jun 27 12:56 ./
drwxr-xr-x 20 ubuntu ubuntu 4096 Jun 27 12:56 ../
drwxr-xr-x 3 ubuntu ubuntu 4096 Jun 26 10:54 .c9/
drwxr-xr-x 8 ubuntu ubuntu 4096 Jun 27 13:05 .git/
-rw-rw-r-- 1 ubuntu ubuntu 948 May 2 07:59 README.md
drwxr-xr-x 12 ubuntu ubuntu 4096 Jun 26 11:04 hello_app/
apori:~/workspace (master) $ cd hello_app/
apori:~/workspace (master) $ pwd
/home/ubuntu/workspace
apori:~/workspace (master) $ git checkout -f
apori:~/workspace (master) $ git status
On branch master
nothing to commit, working directory clean

 apori:~/workspace/hello_app (master) $ ls app/controllers/
application_controller.rb concerns/
apori:~/workspace/hello_app (master) $ 

1.4.3 Bitbucket

 apori:~/workspace/hello_app (master) $ git remote add origin git@bitbucket.org:apori/hello-app.git
apori:~/workspace/hello_app (master) $ git push -u origin --all
Warning: Permanently added 'bitbucket.org,104.192.143.1' (RSA) to the list of known hosts.
Counting objects: 66, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (55/55), done.
Writing objects: 100% (66/66), 17.24 KiB | 0 bytes/s, done.
Total 66 (delta 2), reused 0 (delta 0)
To git@bitbucket.org:apori/hello-app.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
apori:~/workspace/hello_app (master) $ 

 

1.4.4 ブランチ (branch)、変更 (edit)、 コミット (commit)、マージ (merge)

apori:~/workspace/hello_app (master) $ git checkout -b modify-README
Switched to a new branch 'modify-README'
apori:~/workspace/hello_app (modify-README) $ git branch
master
* modify-README

 

1.5 デプロイする

 

1.5.1 Herokuのセットアップ

apori:~/workspace/hello_app (modify-README) $ bundle install --without production
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 columnize 0.9.0
Using debugger-linecache 1.2.0
Using slop 3.6.0
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 1.4.1
Using spring 1.1.3
Using sqlite3 1.3.9
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.3.pre1
Using byebug 3.4.0
Using coffee-script 2.4.1
Using uglifier 2.5.3
Using sprockets 3.6.2
Using sdoc 0.4.0
Using activesupport 4.2.2
Using loofah 2.0.3
Using mail 2.6.4
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.6
Using activemodel 4.2.2
Using jbuilder 2.2.3
Using rails-html-sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using activejob 4.2.2
Using activerecord 4.2.2
Using actionview 4.2.2
Using actionpack 4.2.2
Using actionmailer 4.2.2
Using railties 4.2.2
Using sprockets-rails 3.0.4
Using coffee-rails 4.1.0
Using jquery-rails 4.0.3
Using rails 4.2.2
Using sass-rails 5.0.2
Using web-console 2.0.0.beta3
Using turbolinks 2.3.0
Bundle complete! 14 Gemfile dependencies, 59 gems now installed.
Gems in the group production were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
apori:~/workspace/hello_app (modify-README) $
apori:~/workspace/hello_app (modify-README) $ git commit -a -m "Update Gemfile.lock for Heroku"
[modify-README 10d50cd] Update Gemfile.lock for Heroku
2 files changed, 12 insertions(+), 1 deletion(-)
apori:~/workspace/hello_app (modify-README) $

 

 

apori:~/workspace/hello_app (modify-README) $ heroku version
heroku-toolbelt/3.43.3 (x86_64-linux) ruby/2.3.0
heroku-cli/5.2.20-9d094b0 (linux-amd64) go1.6.2
You have no installed plugins.
apori:~/workspace/hello_app (modify-README) $ heroku login
Enter your Heroku credentials.
Email: myyahamasa@gmail.com
Password (typing will be hidden):
Logged in as myyahamasa@gmail.com
apori:~/workspace/hello_app (modify-README) $ heroku keys:add
Heroku CLI submits usage information back to Heroku. If you would like to disable this, set `skip_analytics: true` in /home/ubuntu/.heroku/config.json
Found an SSH public key at /home/ubuntu/.ssh/id_rsa.pub
? Would you like to upload it to Heroku? Yes
Uploading /home/ubuntu/.ssh/id_rsa.pub SSH key... done
apori:~/workspace/hello_app (modify-README) $ heroku create
Creating app... done, ⬢ polar-cliffs-65958
https://polar-cliffs-65958.herokuapp.com/ | https://git.heroku.com/polar-cliffs-65958.git
apori:~/workspace/hello_app (modify-README) $

 

 

apori:~/workspace/hello_app (modify-README) $ pwd
/home/ubuntu/workspace/hello_app
apori:~/workspace/hello_app (modify-README) $ rails server -b $IP -p $PORT
=> Booting WEBrick
=> Rails 4.2.2 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-07-09 00:41:56] INFO WEBrick 1.3.1
[2016-07-09 00:41:56] INFO ruby 2.3.0 (2015-12-25) [x86_64-linux]
[2016-07-09 00:41:56] INFO WEBrick::HTTPServer#start: pid=4619 port=8080


Started GET "/" for 119.104.157.21 at 2016-07-09 00:42:21 +0000
Processing by ApplicationController#hello as HTML
Rendered text template (0.0ms)
Completed 200 OK in 41ms (Views: 22.8ms | ActiveRecord: 0.0ms)

 

 git remote add heroku git@heroku.com:{my-project-name}.git

 git remote add heroku git@heroku.com:hello_app.git

https://polar-cliffs-65958.herokuapp.com/