New-Village

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

Nitrousでアプリ作成(環境構築編)

アプリケーションのイメージが曖昧なまま、アプリを作っては消してを繰り返していたのだが、毎度毎度、Railsチュートリアルの手順をつまみ食いして、途中で訳が分からなくなってギブアップすることが多かったので、中途半端でも良いからまとめてみる事とした。

ゴルフの練習と温泉にいきたいから、とても中途半端に記事が終わる予定。

アプリケーションの作成

ここは何も問題ないと思う。初期のnitrousのターミナルはosの直下を示しているので、チェンジディレクトリを忘れずに。

~$ cd workspace/ 

~/workspace$ rails new osinco --skip-test-unit 

Gemfileの初期設定

アプリケーションができたら、gemfileをRailsチュートリアルに倣って修正します。

なお、nitrousでは、インストール時にruby 2.1.1をインストールしているので、gemfileを修正しないと怒られます。

~/workspace$ cd appname

~/workspace/appname$ bundle update

~/workspace/appname$ bundle install --without production

gitへの対応

githubソースコードをアップロードするのでセッション変数を暗号化するために使われる秘密トークンを動的に生成するように設定します。

config/initializers/secret_token.rb をリスト3.2の通り修正します。なお、最終行の“SampleApp”は、自分のアプリケーション名に合わせて変更します。

 また、トークンファイル等がgitにアップロードされないように、アプリケーションの直下にある.gitignore(隠しファイル)をリスト1.7の通り修正します。

テストツールのインストール

~/workspace/appname$ rails generate rspec:install

 これでアプリケーション・ディレクトリの配下にspecフォルダが作成されます。

READMEの作成

nitrousだと、アプリケーション・ディレクトリの直下にREADME.rdocがいるので、これをREADME.mdにリネームするだけでOK。内容はご自由に。

マークダウン文法については、この辺を参考に。

文章作成やメモ書きにも便利、Markdown記法 | Web Design KOJIKA17

Githubとの連携

nitrousだと、git configを先に設定しないとコミットしたときに怒られる。

~/workspace/appname$ git config --global user.email "mail address"

~/workspace/appname$ git config --global user.name "github user name"

~/workspace/appname$ git init

~/workspace/appname$ git add . 

~/workspace/appname$ git commit -m"Initial Commit" 

 nitrousのboxesから自分のIDEを開いて、Public Keyをgithubに登録する。自分はgithubのアカウントでログインしているからか、ポチポチボタンを押していれば登録できた。

~/workspace/appname(master)$ git remote add origin git@github.com:UserName/appname.git

~/workspace/appname(master)$ git push -u origin master 

これでgithubに登録完了。

Herokuにアップロード

~/workspace/appname(master)$ heroku login 

~/workspace/appname(master)$ heroku create appname

~/workspace/appname(master)$ git push heroku master

エラー対応:SSHキーの登録

何回Herokuにプッシュしても以下のエラーが出た。リモートレポジトリを登録し直しても怒られるので、ちょっと冷静になって対応したら、すぐに解消されました。

~/workspace/appname(master)$ git push heroku master

Permission denied (publickey).

fatal: Could not read from remote repository.

 

Please make sure you have the correct access rights

and the repository exists.  

~/workspace/appname(master)$ heroku keys:add

エラー対応: rails s が動かない

ローカルでrails serverを動かしてみようと思い、"rails s"コマンドを実行したら以下のエラーが出た。

~/workspace/appname(master*)$ rails s

=> Booting WEBrick

=> Rails 4.0.4 application starting in development on http://0.0.0.0:3000

=> Run `rails server -h` for more startup options 

=> Ctrl-C to shutdown server 

Exiting

/home/action/workspace/appname/config/environments/development.rb:1:in `<top (required)>': undefined method `configure' for #<Osinco::Application:0x000000017b8a80> (NoMethodError) 

from /home/action/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:229:in `require' 

from /home/action/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:229:in `block in require'

from /home/action/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:214:in `load_dependency'

from /home/action/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:229:in `require' 

from /home/action/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/engine.rb:591:in `block (2 levels) in <class:Engine>'     

...

僕らのStackoverflowに似たような事例があったので、これを参考に、appname/config/environments/development.rb の一行目を、

Rails.application.configure do

から

Appname::Application.configure do

に変更したら、無事に動くようになった。

終わり

まだ、 Welcome aboardしか見えないけど今日は終わり。