New-Village

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

Nitrousでアプリ作成(固定ページ作成編)

師曰く、ビジネスにおける保守主義とは「パイを拡大すること」だと言っていた。

シュンペーターイノベーションが、そして、ドラッカーのマネジメントが思い浮かんだが、そこから現実のビジネスに落とし込む事ができずに悶々している。

アイディアはごまんとあるが、継続性のあるビジネスとして育てる為の理論が無い。『戦略課長』を読んで、継続性のあるビジネスを育てるには、会計理論の理解が必須だというところまでは分かったが、同著で紹介されている会計論は製造業的な要素が強いので、まだ腑に落ちない。

なーんて事を考えつつ、平社員は手を動かす。今日は固定ページを作り上げちゃいます。

ブランチの作成

~/$ cd workspace/osinco

~/workspace/appname(master)$ git checkout -b static-pages

固定ページの作成

~/workspace/appname(static-page)$ rails generate controller StaticPages home help --no-test-framework 

~/workspace/appname(static-page*)$ rails generate integration_test static_pages

カピバラを使えるようにする

Rails チュートリアル List 3.10の編集を実施。

テストの作成

動的ページタイトルをテストできるように、spec/support/utilities.rbを作成する。

def full_title(page_title)

  base_title = "appname"

  if page_title.empty?

    base_title

  else

    "#{base_title} | #{page_title}"

  end

end

spec/requests/static_pages_spec.rbを作成して、以下のテスト内容を記載。

require 'spec_helper'

describe "Static pages" do

  subject { page }

 

  describe "Home page" do

    before { visit root_path }

 

    it { should have_content('Sample App') }

    it { should have_title(full_title('')) }

    it { should_not have_title('| Home') }

  end

 

  describe "Help page" do

    before { visit help_path }

 

    it { should have_content('Help') }

    it { should have_title(full_title('Help')) }

  end

end

HTMLの編集

app/views/layouts/application.html.erbをリスト5.13に倣って修正。

リスト5.9のapplication.html.erbは、パーシャルを使っているので、以下のパーシャルを作成。

タイトルを生成する為のヘルパーファイル(app/helpers/application_helper.rb)を作成。

# ページごとの完全なタイトルを返します。

def full_title(page_title)

base_title = "OSINCO"

if page_title.empty?

  base_title

else

  "#{base_title} | #{page_title}"

end

end

app/views/static_pages/home.html.erb と help.html.erb を、Railsチュートリアルリスト5.2リスト3.28を参考に修正。

テストが通ればOK。

~/workspace/appname(static-page*)$ rspec spec

CSSの設定

GemfileにBootstrapを追加して、bundle installを実行。

gem 'bootstrap-sass', '~>2.3.2.0'

~/workspace/appname(static-page*)$ bundle install

app/assets/stylesheets/custom.css.scssを作成して、リスト5.16の通りに設定する。

Asset Pipelineの設定

正直、Asset Pipelineの役割は良く理解していないが、後でエラーを吐かれるのも嫌なので設定しておく。

config/application.rbにAsset Pipeline互換の行を追加する。Railsチュートリアルリスト5.4を参考にした。

マニフェストが設定されていない場合は、app/assets/stylesheets/application.cssリスト5.15の通り設定する。

エラー: Bootstrapをインストールしてからエラーが出るようになった

全てのテストにおいて、以下のエラーが出るようになってしまった。 

1) Static pages Home page に'appname'の文字列が存在する。

     Failure/Error: visit '/static_pages/home'

     ActionView::Template::Error:

       undefined method `environment' for nil:NilClass

         (in /home/action/workspace/appname/app/assets/stylesheets/custom.css.scss)

     # ./app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___2366206669483793978_23875960'

     # ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>' 

僕らのStackoverflowを参考に、gemfileに記載されているsass-railsを以下のように書き換えて、bundle updateしたら解消された。

gem 'sass-rails', '~>4.0.1'

~/workspace/appname(static-page*)$ bundle update

ルートファイルの設定

config/routes.rbを以下の通り設定する。

root  'static_pages#home'

match '/signup',  to: 'users#new',            via: 'get'

match '/help',    to: 'static_pages#help',    via: 'get'

最後に

GithubとHerokuにアップロード。

~/workspace/appname(static-page*)$ git add .

~/workspace/appname(static-page)$ git commit -m "Finish static pages"

~/workspace/appname(static-page)$ git checkout master

~/workspace/appname(master)$ git merge static-page

~/workspace/appname(master)$ git push

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

とりあえず、ここで休憩...と思ったけど...git push herokuがエラーを吐いたので、additional Timeに突入。

エラー: git push heroku中にエラー

push中にbundle installをしているが、ここで以下のようなエラーが発生した。

-----> Ruby app detected

-----> Compiling Ruby/Rails

-----> Using Ruby version: ruby-2.1.1

-----> Installing dependencies using 1.5.2

Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment

(中略)

Removing activerecord (4.1.0)

-----> Writing config/database.yml to read from DATABASE_URL

-----> Preparing app for Rails asset pipeline

Running: rake assets:precompile

rake aborted!

NoMethodError: undefined method `dump_schema_after_migration=' for ActiveRecord::Base:Class

(以下略)

またまた、僕らのStackoverflowを参考に、config/environments/production.rbの以下の行をコメントアウト(#)して、

config.active_record.dump_schema_after_migration = false

再度、GithubとHerokuにアップロード。

~/workspace/appname(master*)$ git add .

~/workspace/appname(master)$ git commit -m "Support Heroku Upload"

~/workspace/appname(master)$ git push

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

とりあえず、エラーで検索かけるとStackoverflowが引っかかるから凄い。これでようやく休憩できます。