LoginSignup
22
23

More than 5 years have passed since last update.

あとでもっかい試す用 - Ruby on Rails 4.0 チュートリアル - 第一章

Posted at

Ruby on Rails チュートリアル 実例を使ってRailsを学ぼう / Michael Hartl (マイケル・ハートル) を見ながらやったことを、あとでもう一度できるようにまとめます!!

第一章の説明

第1章では、必要なソフトウェアをインストールし、開発環境 (1.2) を整えて Ruby on Rails を動かす準備をします。その後、first_app という最初の Rails アプリの作成に着手します。Rails チュートリアルでは、ソフトウェア開発のベストプラクティスを重要視しているので、新しい Rails プロジェクトを作成した後に、すぐに Git (1.3) を使ったバージョン管理を実践してもらいます。そして最後に、本番 (production) 環境 (1.4) にデプロイして、作成したアプリを一般公開するところまで一通り実践します。

チュートリアルはじめ!Railsプロジェクトを作る。

rails プロジェクトを作成

$ mkdir -p ~/workspace/ruby/rails && cd ~/workspace/ruby/rails/
$ rails new first_app
$ cd first_app

Gemfileを編集する。gemを更新するとうまく動かないことがあるので、特定のバージョンを指定する。

diff --git a/Gemfile b/Gemfile
index 30d54d4..407f7dd 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,25 +1,29 @@
 source 'https://rubygems.org'
+ruby '2.0.0'
+#ruby-gemset=railstutorial_rails_4_0

 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '4.0.0'

 # Use sqlite3 as the database for Active Record
-gem 'sqlite3'
+group :development do
+  gem 'sqlite3', '1.3.7'
+end

 # Use SCSS for stylesheets
-gem 'sass-rails', '~> 4.0.0'
+gem 'sass-rails',   '4.0.0'

 # Use Uglifier as compressor for JavaScript assets
-gem 'uglifier', '>= 1.3.0'
+gem 'uglifier', '2.1.1'

 # Use CoffeeScript for .js.coffee assets and views
-gem 'coffee-rails', '~> 4.0.0'
+gem 'coffee-rails', '4.0.0'

 # See https://github.com/sstephenson/execjs#readme for more supported runtimes
 # gem 'therubyracer', platforms: :ruby

 # Use jquery as the JavaScript library
-gem 'jquery-rails'
+gem 'jquery-rails', '2.2.1'

 # Turbolinks makes following links in your web application faster. Read more: h
 gem 'turbolinks'

bundleを更新

$ bundle update
$ bundle install

Railsを動かしてみる

$ rails server

ブラウザで http://localhost:3000 を開く。

Gitをセットアップ

$ git config --global user.name "あなたの名前"
$ git config --global user.email your.email@example.com
$ git config --global alias.co checkout # git checkout のエイリアスを設定

RailsプロジェクトのGit設定

$ cd ~/workspace/ruby/rails/first_app
$ git init
$ subl .gitignore

.gitignore に設定を追加

$ git diff .gitignore 
diff --git a/.gitignore b/.gitignore
index 25a742d..01ce905 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,3 +14,12 @@
 # Ignore all logfiles and tempfiles.
 /log/*.log
 /tmp
+
+# Ignore other unneeded files.
+doc/
+*.swp
+*~
+.project
+.DS_Store
+.idea
+.secret

READMEを修正

修正

# ブランチを作成
$ git co -b modify-README
# READMEをmarkdownの拡張子に変更
$ git mv README.rdoc README.md
# 内容変更
$ subl README.md
# コミット!
$ git commit -a -m "Improve the README file"

マージ

$ git co master
# マージ
$ git merge modify-README --no-ff
# ブランチ削除
$ git branch -d modify-README

Github の設定

※ Githubのリポジトリは作っておいて、SSHの設定は終わらせてある。

$ git remote add origin git@github.com:yujiroarai/first_app.git
$ git push -u origin master

Heroku にデプロイ

HerokuでPostgreSQLを使う為に、Gemfileを修正

diff --git a/Gemfile b/Gemfile
index 407f7dd..bcfcb81 100644
--- a/Gemfile
+++ b/Gemfile
@@ -47,3 +47,8 @@ end

 # Use debugger
 # gem 'debugger', group: [:development, :test]
+
+group :production do
+  gem 'pg', '0.15.1'
+  gem 'rails_12factor', '0.0.2'
+end

bundleを更新する。本番用のGemはインストールしないようにする。
Gemfile.lockは更新する

$ bundle install --without production
# コミット
$ git commit -a -m "Update Gemfile.lock for Heroku"

heroku にアップロード

$ heroku login
$ heroku create
$ git push heroku master
$ heroku open # The page you were looking for doesn't exist.ってエラーが出るけど気にしない

herokuアプリケーションの名前を変更

heroku rename yujiroonrails
22
23
0

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
22
23