実践Rails学習ノート#3【Viewで基礎を作る】

前回に続きまして実践Ruby on Railsの学習ノートの続きです。

今回はViewのデザインをしながらビジュアルの部分をまず固めていくような内容です。

名前空間を分ける

このアプリは

  • 開発者
  • 顧客
  • ユーザ

の三者の利用を考えているので、名前空間を分けておきます。

# config/routes.rb
Rails.application.routes.draw do
  namespace :staff do
    root 'top#index'
  end

  namespace :admin do
    root 'top#index'
  end

  namespace :customer do
    root 'top#index'
  end
end

Controller生成

次にControllerをつくります。

さっき区切った名前空間に従ってcontrollerを生成します。

$ rails g controller staff/top
$ rails g controller admin/top
$ rails g controller customer/top

自分の場合は、bin/をつけると

$ bin/rails g controller staff/top
env: ruby2.2: No such file or directory

とエラーが出て動いてくれませんでした。bin/railsを使うクセはつけていきたいなぁと思うんだけど、何が問題なのだろう...。

Rails 4.1から導入された、bin/については以下で詳しくまとめてくださっています。

Rails 4.1以降のコンソールコマンドは必ず bin/ を付けなきゃいけないの? - Qiita

それぞれ以下のようにディレクトリ、ファイルが作成されました。

$ rails g controller staff/top
      create  app/controllers/staff/top_controller.rb
      invoke  erb
      create    app/views/staff/top
      invoke  rspec

それぞれのcontrollerに、indexメソッドを加えます。

# app/controller/customer/top_controller.rb
class Customer::TopController < ApplicationController

  def index
    render action: 'index'
  end

end

このように、パブリックになっているインスタンスメソッドのことをRailsではアクションと呼びます。

ちなみにrenderメソッドを使ってアクションを指定しなくても、デフォルトでアクション名に応じてerbテンプレートを呼び出してくれます。

secret keyを設定する

assetsのプリコンパイルのために、secret keyを設定します。ここからはゲストOSで作業します。

本でこのようなRubyワンライナーが紹介されてて、おお、なってなりました。すげー

$ ruby -e 'require "securerandom"; print SecureRandom.hex(64)' > ~/.baukis_secret_key_base

これを環境変数にぶちこみます。

$ export SECRET_KEY_BASE=`cat ~/.baukis_secret_key_base`

config/database.ymlのproduction時の設定を以下のように書き換えました。

username: user
password: 'password'

Railsの環境をproductionでmigrationファイルからdbを生成します。

$ bin/rake db:create RAILS_ENV=production

以下を、config/environments/production.rbに加えます。

  # return asset files to a browser
  config.serve_static_assets = true

これは、アセットファイルをブラウザーに返すかどうかの設定で、本来はApacheやnginxなどのWebサーバーの役割なのでfalseでいいのですが、今回はRailsアプリに直接アクセスするので、trueにしておきます。

以下rails guidesからの引用です。

  • config.serve_static_files configures Rails itself to serve static files. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
$ bin/rake assets:precompile

正常にassetsなども読み込まれて表示されればokです!

ちなみにbin/rails sしたら以下のようなメッセージが表示されました。rails 5.0からは名前が変わるんですね〜。

DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in at /vagrant/baukis/config/environments/production.rb:81)

今回のdiff

https://github.com/totzYuta/baukis/compare/021208bd387f26c9c8cfcfc71ed5699b894292ae...53e43f189976083b354a94c752f292223c7cd569