LoginSignup
32
35

More than 5 years have passed since last update.

Railsプロジェクトを作ったときにまずやる事

Posted at

はじめに

前回の記事に書いたもののプロジェクト編です。

Railsプロジェクトを作成した後にほぼ必ず行う事を記載しておきます。

Railsプロジェクト作成

必ずやる事ですが、たまにコマンドを忘れるので一応メモ

$ rails new プロジェクト名 -d mysql

application.rbの変更

i18nの階層対応とタイムゾーンの設定をします。

config/application.rb
module アプリ名
  class Application < Rails::Application
    # i18nファイルを階層管理できるようにします。(後述)
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]

    # 表示時のタイムゾーンをJSTに変更
    config.time_zone = 'Tokyo'
    # DB保存時のタイムゾーンをJSTに変更
    config.active_record.default_timezone = :local

    # ロケールを日本語に変更
    config.i18n.default_locale = :ja

    ...
  end
end

日本語化対応

ja.ymlを日本語化します。
このあたりから取得してきます。

$ wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml -P config/locales/

application.rblocalesを読み込むようにしたので各モデルはconfig/locales/modelsフォルダを掘って管理する。

※小規模ではここまでしなくても良いのですが、たいした手間ではないので階層化して管理してると後々楽に管理できる。

各モデルの記述は以下のようになってスマート

confilg/locales/models/user/ja.yml
ja:
  activerecord:
    attributes:
      # ユーザー情報
      user:
        account: アカウント
        password: パスワード

Viewで使うときは以下のようになる。

<%= t 'activerecord.attributes.user.account' %>

bootstrapを入れる

管理画面はほぼこれで作る。
ただ、入れるときにはGemでは入れない。

  <!-- Bootstrap core CSS -->
  <%= stylesheet_link_tag "/css/bootstrap.min.css" %>
  <%= javascript_include_tag '/js/bootstrap.min.js' %>

こんな感じで必要に応じた(管理画面)だけに入れる。
assetsで管理すると楽だけど、Bootstrapのバージョンアップとかが地味に面倒だったりフロントデザインと競合したりすると面倒なので。

この方法だととても楽に管理できる。

flashメッセージ表示用ヘルパーを作る

大したものではないけど、flashメッセージは毎回表示するので作ってる。

  # メッセージの種別によって振り分けて表示します。
  # success => alert-success
  # info => alert-info
  # alert, warn => alert-warning
  # danger, error => alert-danger
  def flash_message
    message = nil
    tag = nil
    {
        success: "alert-success",
        info: "alert-info",
        alert: "alert-warning",
        warn: "alert-warning",
        danger: "alert-danger",
        error: "alert-danger",
    }.each { |key ,value|
      if flash[key]
        message = flash[key]
        tag = value
        break
      end
    }
    "<div class='alert #{tag}' role='alert'>#{message}</div>".html_safe if message
  end

使用方法は以下を好きなところに入れる。

<%= flash_message %>

モデルエラーメッセージ用のヘルパーを作る。

これも同様

  # エラーメッセージを表示します。
  def error_message_with_model(model)
    render partial: "model/error_model", object: model if model
  end

レイアウトは以下

views/model
<% if error_model.errors.any? %>
    <div id="error_explanation">
      <div class="alert alert-danger">
        <%= error_model.errors.count %>件のエラーがあります。
        <ul>
          <% error_model.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
          <% end %>
        </ul>
      </div>
    </div>
<% end %>

使い方は以下を好きなところに入れる。

<%= error_message_with_model(@model) %>

turbolinksの調査

よく忘れるので以下のコードを使って、ターボリンクの挙動を調査する。
今更ながらTurbolinksを初めて仕事で使ってみたので色々調べてみたさんのコードを使わせてもらってます。

#$(document).on 'page:before-change' , -> console.log 'page:before-change'
#$(document).on 'page:fetch'         , -> console.log 'page:fetch'
#$(document).on 'page:receive'       , -> console.log 'page:receive'
#$(document).on 'page:change'        , -> console.log 'page:change'
#$(document).on 'page:update'        , -> console.log 'page:update'
#$(document).on 'page:load'          , -> console.log 'page:load'
#$(document).on 'ready page:load'    , -> console.log 'ready and load'
#$(document).ready                     -> console.log '$(document).ready in assets'
#$(window).on 'load'                 , -> console.log '$(window).load in assets'

その他

思い出したら追記します。

32
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
35