ぞえの技術めも

Ruby on Rails勉強中

【3日目】【1日20分のRailsチュートリアル】Hello, world!からGitのセットアップまで

Ruby on Railsチュートリアル(第3版)

今日はMVCとは、から。

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

1.3.3 Model-View-Controller (MVC)

MVCはなんとなく知ってるので軽く文章読むのみ。
後で理解を深める。

1.3.4 Hello, world!

2日目で動かしたアプリで「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
end

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

を追加する。(最後のendの前)

次はroutes.rbの設定。
どのコントローラのどのアクションをルートにするかを設定する。

config/routes.rb 6行目の

  # root 'welcome#index'

を削除して、

  root 'application#hello'

を追加する。
削除しなくても問題ないが、削除することでroutes.rbを見れば今のルーティングがぱっと見分かりやすい。(ってだけだと思う)

これでソースの修正は終わり。 日を跨いでいるのでサーバを起動して

$ cd hello_app/
$ rails server -b $IP -p $PORT

「Hello, world!」が表示されました。

f:id:kt_zoe:20160711123633p:plain

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

Gitは仕事でも使うのでさらっといく。

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

クラウドIDEにはGitがインストールされてるそうなのでインストールは不要。

初めてのシステムセットアップ

名前とメールアドレスを設定。

$ git config --global user.name "<設定したいユーザー名>"
$ git config --global user.email <設定したいメールアドレス>
$ git config --global push.default matching

ユーザー名は半角英数で設定した方がいいと思う。(日本語設定できるのかな…)

$ git config --global alias.co checkout

はGitのコマンドの"checkout"→"co"と略せるようにする設定なので今回はしない。

ちなみに

$ git config --global -l

で今の設定状態を確認できます。

初めてのリポジトリセットアップ

下記コマンドを実行してさくっと完了。

$ git init
Initialized empty Git repository in /home/ubuntu/workspace/hello_app/.git/
$ git add -A
$ git commit -m "Initialize repository"
[master (root-commit) dc58225] Initialize repository
 57 files changed, 968 insertions(+)
 create mode 100644 .gitignore
  :

1.4.2 Gitのメリット

Gitはよく使うのでこの章はスルー。

今日の作業時間は【24分】でした。

次は「1.4.3 Bitbucket」から。