Railsチュートリアルを進めていく上での備忘録。
環境
PC:MacBook Pro
IDE:AWSCloud9
Rilsバージョン:5.1.6
Rubyバージョン:2.6.3
Cloud9を使用する場合、c9コマンドが使えると便利なので、インストールしておく。
$ npm install -g c9
作業しやすい様にbashrcも変更しておく。
# .bashrc
export PATH=$PATH:$HOME/.local/bin:$HOME/bin
# load nvm
export NVM_DIR="$HOME/.nvm"
[ "$BASH_VERSION" ] && npm() {
# hack: avoid slow npm sanity check in nvm
if [ "$*" == "config get prefix" ]; then which node | sed "s/bin\/node//";
else $(which npm) "$@"; fi
}
# [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
rvm_silence_path_mismatch_check_flag=1 # prevent rvm complaints that nvm is first in PATH
unset npm # end hack
# User specific aliases and functions
alias python=python3.6
# modifications needed only in interactive mode
if [ "$PS1" != "" ]; then
# Set default editor for git
git config --global core.editor /usr/bin/nano
# Turn on checkwinsize
shopt -s checkwinsize
# keep more history
shopt -s histappend
export HISTSIZE=100000
export HISTFILESIZE=100000
export PROMPT_COMMAND="history -a;"
# Source for Git PS1 function
git_type=$(type -t __git_ps1)
if [ -z $git_type ] && [ -e "/usr/share/git-core/contrib/completion/git-prompt.sh" ]; then
. /usr/share/git-core/contrib/completion/git-prompt.sh
fi
# Cloud9 default prompt
_cloud9_prompt_user() {
if [ "$C9_USER" = root ]; then
echo "$USER"
else
echo "$C9_USER"
fi
}
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
git_color() {
[[ -n $(git status --porcelain=v2 2>/dev/null) ]] && echo 31 || echo 33
}
# PS1='\[\033[01;32m\]$(_cloud9_prompt_user)\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)" 2>/dev/null) $ '
#PS1="╭─○ \[\033[01;32m\]${C9_USER}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)")╰─○ "
PS1="╭─○ \[\033[\$(git_color)m\]\$(git_branch)\[\033[00m\]\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)")
╰─○ "
fi
[[ -s "$HOME/.rvm/environments/default" ]] && source "$HOME/.rvm/environments/default"
# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"
peco-select-history() {
declare l=$(HISTTIMEFORMAT= history | sort -k1,1nr | perl -ne 'BEGIN { my @lines = (); } s/^\s*\d+\s*//; $in=$_; if (!(grep {$in eq $_} @lines)) { push(@lines, $in); print $in; }' | peco --query "$READLINE_LINE")
READLINE_LINE="$l"
READLINE_POINT=${#l}
}
bind -x '"\C-r": peco-select-history'
#alias hub-pr="hub pull-request | open"
#alias git="hub"
alias ping-loop="while true; do ping www.google.com; sleep 3; done;"
alias gommit="git commit"
alias tag-gen="ripper-tags -e -R -f TAGS"
alias h="heroku"
alias la="ls -a"
alias lf="ls -F"
alias ll="ls -l"
alias lv="less"
alias e="emacs -nw"
alias g="git"
alias ga="git add -A"
alias gc="git commit -m"
alias gp="git push"
#alias gs="git status"
alias gd="git diff"
alias gl="git log --pretty='format:%Cblue[%ad] %Cgreen%an %Creset%s' --date=short"
alias gr="git remote -v"
alias v="vim"
alias r="rails"
alias bi="bundle install"
alias be="bundle exec"
変更したbashrcを反映する。
source ~/.bashrc
Gemとは
gem
Rubyで使用することのできる汎用性の高い機能をまとめたライブラリを管理するシステムの事を言う。また、それぞれのライブラリの事をgemという。
Bundler
Bundlerはgemを管理するためのgemです。
Gemfile
gemをインストールするための「設計図」のようなもの。
自身のアプリケーションに必要なgemを記述する。
Gemfile.lock
実施にgemをインストールした後の、「結果図」のようなもの。
gem同士は関連しあっていることが多いので、Gemfileに書いてあるgemの他にも必要なgemが生じます。Bundlerは、それらを自動でインストールし、Gemfile.lockに記述する。
bundle installとbundle updateの違い
bundle install
Gemfile.lockを元にgemのインストールを行う。
この時、Gemfile.lockに記述されていない、且つGemfileに記述されているgemが存在する場合、そのgemとそgemに関連するgemをインストール後、Gemfile.lockが更新される。
bundle update
Gemfileを元にインストールを行う。
その後、Gemfile.lockを更新する。
withoutオプション
$ bundle install --without 環境名 ⇨指定した環境のgemをインストールしなくできる
また、このオプションを一度実行すると、「.bundle/config」に設定が保存され、今後「bundle install」を実行する時に「--without」オプションを追加する必要がなくなる。
$ cat .bundle/config
---
BUNDLE_WITHOUT: "production"
githubへのプッシュ
$ git init ⇨初期化
$ git add -A ⇨プロジェクトのファイルをリポジトリに全て追加
$ git commit -m "Initialize repository" ⇨リポジトリにコミット
↓公開鍵が作成されている事
$ git remote add origin git@github.com:s-yoshi210/hello_app.git ⇨リポジトリ追加
$ git push -u origin master ⇨リポジトリへのプッシュ
Herokuへのデプロイ
$ source <(curl -sL https://cdn.learnenough.com/heroku_install) ⇨Herokuインストール
$ heroku login --interactive ⇨Herokuログイン
$ heroku keys:add ⇨SSHキー追加
$ heroku create ⇨Herokuサーバーにアプリケーションの実行場所を作成
$ git push heroku master ⇨Herokuにリポジトリをプッシュ
Herokuのアプリケーション名変更
$ heroku rename 任意のアプリ名 ⇨但し、既に誰かによって使われている名称は使用不可
Herokuのアプリケーション削除
$ heroku apps:destroy --app アプリ名 --confirm アプリ名