New-Village

月間ブログ。だいたい1カ月に1回は更新しているようです。

アプリケーション作成(初期環境構築)

既に別の記事で同じような内容をまとめてあるが、内容を整理し直したので書き直した。

また、Ruby on Railsチュートリアルrails 4.0)のgemfileでは、bootstrap2を使っているが、今回の初期環境構築ではbootstrap3を導入した。

このbootstrapのバージョンアップによって、クラス名の変更が行われたため、固定ページの内容も修正が必要になる。ここ数週間、はじめは軽い気持ちでバージョンアップしたが、この整合性を取る為に色々と試行錯誤を繰り返した。

ともあれ、本記事では初期環境の構築として、gemを設定してインストール、あとはgithub, herokuとの連携について記載する。

 

■ アプリケーションの作成

NitrousでBOXを作成したら、まずはRailsプロジェクトを作ります。

~$ cd workspace/
~/workspace$ rails new xapp9 --skip-test-unit
~/workspace$ cd xapp9


■ gemfileの設定

アプリケーションができたら、gemfileをRailsチュートリアルに倣って修正します。
なお、nitrousでは、インストール時にruby 2.1.1をインストールしているので、gemfileを修正しないと怒られます。

./gemfile

source 'https://rubygems.org'
ruby '2.1.1'
#Railsチュートリアル リスト3.1をベースとしている
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.1.0'
gem 'bootstrap-sass', '~> 3.2'
gem 'sprockets', '~>2.11'
gem 'sass-rails', '~>4.0.2'
gem 'uglifier', '~>2.1'
gem 'coffee-rails', '~>4.0'
gem 'jquery-rails', '~>3.0'
gem 'turbolinks', '~>1.1'
gem 'jbuilder', '~>1.0'

group :development, :test do
  gem 'sqlite3', '~>1.3'
end

group :test do
  gem 'selenium-webdriver', '~>2.35'
  gem 'rspec-rails', '~>2.13'
  gem 'capybara', '~>2.1'
end

group :doc do
  gem 'sdoc', '~>0.3', require: false
end

group :production do
  gem 'pg', '~>0.15'
  gem 'rails_12factor', '~>0.0.2'
end

gemfileを設定したらインストールを実行します。また、rspecの初期設定を行います。

~/workspace/xapp9$ bundle install --without production
~/workspace/xapp9$ bundle update
~/workspace/xapp9$ bundle install
~/workspace/xapp9$ rails g rspec:install


■ Readmeの編集

アプリケーション・ディレクトリの直下にREADME.rdocがいるので、これをREADME.mdにリネームします。その後内容を編集します。
マークダウン文法については、この辺を参考に。

Markdown記法 チートシート - Qiita


githubへの対応

githubソースコードをアップロードするのでセッション変数を暗号化するために使われる秘密トークンを動的に生成するように設定するファイルを作成します。
なお、ファイルの末尾にある"Xapp9"は自分のアプリケーション名に変更します。

./config/initializers/secret_token.rb

# Be sure to restart your server when you modify this file.

# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!

# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rake secret` to generate a secure secret key.

# Make sure your secret_key_base is kept private
# if you're sharing your code publicly.
require 'securerandom'

def secure_token
 token_file = Rails.root.join('.secret')
 if File.exist?(token_file)
  # Use the existing token.
  File.read(token_file).chomp
 else
  # Generate a new token and store it in token_file.
  token = SecureRandom.hex(64)
  File.write(token_file, token)
  token
 end
end

Xapp9::Application.config.secret_key_base = secure_token

 

次に、トークンファイル等がgithutにアップロードされないようにアプリケーションの直下にある.gitignore(隠しファイル)を修正します。Nitrousでは画面左のメニューにある"Show Hidden"を押すと、隠しファイルが表示できます。

./.gitignore

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# Ignore other unneeded files.
doc/
*.swp
*~
.project
.DS_Store
.idea
.secret

 

gitレポジトリの作成と最初のコミットを実施します。

~/workspace/xapp9$ git init
~/workspace/xapp9$ git config --global user.email "mail address"
~/workspace/xapp9$ git config --global user.name "github user name"
~/workspace/xapp9$ git add .
~/workspace/xapp9$ git commit -m"初期環境構築"


githubとの連携設定

nitrous IDE画面のメニューバーからBoxesを開きます。画面が遷移したら現在開発しているアプリのboxを開きます。boxを開いたら"Public Key"項目から、"Reveal Public Key"を選択します。暗号鍵が生成されたら"Add to github"を選択してgithubに暗号鍵を登録します。

暗号鍵の設定が終わったら、githubにファイルをアップロードします。

~/workspace/xapp9(master)$ git remote add origin git@github.com:Kzki/xapp9.git
~/workspace/xapp9(master)$ git push -u origin master


■ Herokuへのアップロード

nitrousはherokuクライアントがデフォルトでインストールされているので、セットアップしてからgitリポジトリをアップロードします。

~/workspace/xapp9(master)$ heroku login
Enter your Heroku credentials.
Email:
Password (typing will be hidden):
Authentication successful.
~/workspace/xapp9(master)$ heroku create xapp9
~/workspace/xapp9(master)$ heroku keys:add
~/workspace/xapp9(master)$ git push heroku master