LoginSignup
5

More than 5 years have passed since last update.

Ruby on Rails環境構築メモ② - Ruby on Railsのインストール、初期設定

Last updated at Posted at 2016-03-30

Ruby on Rails環境構築メモ

目次
rbenv,ruby,mysqlのインストール
② ruby on railsのインストール


Ruby on Rails環境構築メモ② - Ruby on Railsのインストール

railsのインストール

$ mkdir project
$ cd project
$ bundle init
Writing new Gemfile to /path/to/project/Gemfile

Gemfileのgem "rails"の部分のコメントアウトを外す。

Gemfile
$ vi Gemfile
# A sample Gemfile
source "https://rubygems.org"

gem "rails"
  • bundle install
$ bundle install --path vendor/bundle
$ bundle exec rails new -d mysql .

Overwrite /home/user/project/Gemfile? (enter "h" for help) [Ynaqdh]Y

OverwriteはYを押して許可してください。

Gemfile

下記を追記してbundle install

# unicorn
gem 'unicorn'

# bootstrap
gem 'less-rails'
gem 'twitter-bootstrap-rails'
gem 'therubyracer', platforms: :ruby

# haml
gem 'haml-rails'
gem 'erb2haml'

# paging
gem 'kaminari'

group :development, :test do
  gem 'dotenv-rails'
end
$ bundle install --path vendor/bundle

bundle installでエラーが出る場合

こんなエラーが出ることがありますが、gem install nokogiriを実行しても解決しません。

An error occurred while installing nokogiri (1.8.4), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.4' --source 'https://rubygems.org/'` succeeds before bundling.

以下を実行すると解決します。

$ bundle config --local build.nokogiri --use-system-libraries
You are replacing the current local value of build.nokogiri, which is currently nil

参考:Bundlerでビルドオプションを指定する

haml適用

scaffoldで生成されるviewファイルがhamlで生成されるようにする

テンプレートをhamlに変換

bash
$ bundle exec rake haml:replace_erbs

これでscaffoldすれば、viewがhamlで生成される

bash
$ bundle exec rails g scaffold Hoge name:string age:integer

bootstrap適用

bootstrapのインストール

bash
$ bundle exec rails g bootstrap:install

Hogeアプリケーションのviewを、bootstrapが適用されたものに書き換え。
既存のviewファイルが上書きされますので、既にviewに変更を加えている場合は気をつけてください。

bash
$ bundle exec rails g bootstrap:themed Hoge

Overwrite ~? (enter "h" for help) [Ynaqdh]Y
が出たらすべてYで許可する。

form submit時のエラー表示変更

下記をconfig/application.rbに追記

config/application.rb
config.action_view.field_error_proc = proc { |html_tag, instance| "<div class='has-error'>#{html_tag}</div>".html_safe }

参考:Railsで自動で挿入されるエラー出力タグ <div class="field-with-errors"></div>の変更

その他

scaffold削除

bash
$ bundle exec rails destroy scaffold Hoge

autoload, locale設定

libs, validatorsディレクトリがautoloadされるようにし、
localeを日本語にする。

config/application.rb
    # libs
    config.autoload_paths += %W(#{config.root}/lib)
    # validators
    config.autoload_paths += %W(#{config.root}/app/validators)

    # locale
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :ja

localeファイル作成

localeファイルの作成

activerecord用

config/locales/ja.yml
# conifg/locales/ja.yml
ja:
  activerecord:
    models:
      hoge1: hoge1一覧
      hoge2: hoge2一覧
    attributes:
      hoge1:
        id: id
        entry_id: entry_id
        name: 名前
        age: 年齢
        description: メモ
        enabled: 有効/無効
        created_at: 作成日
      hoge2:
        id: id
        entry_id: entry_id
        name: 名前
        age: 年齢
        description: メモ
        enabled: 有効/無効
        created_at: 作成日

viewで下記の用に記述するとそれぞれ使用できる。

views/hoge/index.html.haml
model_class.human_attribute_name(:name)

localeファイルを分離する

ja.yml以外にファイルを生成しても、autoloadされる。
下記は、ja.global_menu.ymlを作成する例。

config/locales/ja.global_menu.yml
# conifg/locales/ja.menu.yml
ja:
  global_menu:
    title: 'hoge管理画面'
    li:
      hoge1: 'hoge1一覧'
      hoge2: 'hoge2一覧'

viewで下記の用に記述するとそれぞれ使用できる。

views/hoge/global_menu.html.haml
t('global_menu.title')
t('global_menu.li.hoge1')
t('global_menu.li.hoge2')

以上。

[参考]
システムのgemにrailsをインストールせずrails newする
設定ファイル内のパスワード等を環境変数で隠すための How to 色々

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
5