lepetit

自分の勉強メモ

RoRチュートリアル第1章

Web開発フレームワークRuby on Railsチュートリアルに挑戦!!
railstutorial.jp


早速railsをインストーーーーーーール
何故かsudoつけないとできなかった…普通はつけないでもできるっぽい;;

$ sudo gem install rails

今回はバージョン4.2.6が入ったのでそれでやっていきま!

まずは,Rails勉強用にworkspaceディレクトリを作ってそこに移動

$ mkdir workspace
$ cd workspace/

アプリケーションとか必要なやつをパパーっとインストールする
(指定するバージョン番号に注意よ)

$ rails _4.2.6_ new hello_app

hello_appファイル作成後にbundle install コマンドが自動的に実行されているんだけど,
bundle installはアプリケーションに必要なgemをインストールしてくれる便利('ロ'('ロ'('ロ'('ロ' )!!!
(gemはRubyに標準で入ってるライブラリのことだそう…)

hello_appディレクトリにあるGemfileを以下のように書き換える。
(ここでもrailsのバージョンに注意)

source 'https://rubygems.org'

gem 'rails',                '4.2.6'
gem 'sass-rails',           '5.0.2'
gem 'uglifier',             '2.5.3'
gem 'coffee-rails',         '4.1.0'
gem 'jquery-rails',         '4.0.3'
gem 'turbolinks',           '2.3.0'
gem 'jbuilder',             '2.2.3'
gem 'sdoc',                 '0.4.0', group: :doc

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

次に,hello_appディレクトリ上でbundle installする

$ cd hello_app/
$ bundle install

これでアプリケーションが実行可能になった!確認してみよう
ディレクトリはhello_appのままで,別ターミナルタブで以下を実行

$ rails server

↓ブラウザで開いてみて〜〜
http://localhost:3000/

↓こうなってたらできてるb
f:id:iris15:20160630155835p:plain



ここで…Railsアプリケーションの全体的な仕組みとしてModel-View-Controller(MVC)を知っておこう。
MVCってこんな感じ!
f:id:iris15:20160630160935p:plain



アプリケーションに戻って,webページに「hello, world!」を表示させてみる。
app/controllers/ ディレクトリのapplication_controller.rbに書き足して以下のようになったら

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def hello
    render text: "hello, world!"
  end
end

config/routes.rbの以下の部分を

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

次のように書き換える。

  # You can have the root of your site routed with "root"
  root 'application#hello'

はいチェック! http://localhost:3000/

f:id:iris15:20160701095734p:plain

できてる♪( 'ω' و(و"



新しく動作するRailsアプリが完成したところで,Gitによるバージョン管理を行っていこう!

初期設定は
Gitをインストールしたら真っ先にやっておくべき初期設定 - Qiita
を参考に設定しましたb

初めてのリポジトリセットアップ〜〜として,まずhello_appディレクトリ上で以下を実行

$ git init
$ git add -A

リポジトリを作成し, すべての変更を追加する。

変更されたファイルの一覧を確認したいときは,

$ git status

で見ることができる。

コメント付きで変更点を更新する。

$ git commit -m "initialize repository"


続いて,アプリケーションを実行するためのプラットフォームであるHerokuのセットアップ

workspace/hello_app/ の中のGemfileの最後に以下を書き足す。

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end

次に

$ bundle install --without production
$ git commit -a -m "Update Gemfile.lock for Heroku"
$ heroku version

をそれぞれ実行したらHerokuにログインしてSSHキーを追加し,新しいアプリケーションを作成する。

$ heroku login
$ heroku keys:add
$ heroku create

Herokuにデプロイ!

$ git push heroku master


っしゃ〜〜〜とりあえず1章おわり\(*ˊᗜˋ*)/