【Rails】俺好みのrails new備忘録

rails new して色々試すときにあれ?いつも何やるっけってことをなくすために、rails newのときによくすることを備忘録として残しました。

rails new

rails new sandbox -d postgresql -j esbuild --css bootstrap --skip-test

I18n

  class Application < Rails::Application
    ...
    # デフォルトタイムゾーンを日本時間に
    config.time_zone = 'Tokyo'
    # Railsアプリのデフォルト言語を日本語に
    config.i18n.default_locale = :ja
# config/localesにja.ymlをダウンロード
curl -s https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml -o config/locales/ja.yml

html2hamlでerbをhaml

globalにインストールしているhtml2hamlをプロジェクト配下で実行。

GitHub - haml/html2haml: Convert HTML and HTML+Erb to Haml.

find . -name \*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 html2haml

gem

gem 'devise'
gem 'simple_form'
gem 'haml-rails'
gem 'enumerize'

group :test do
  ...
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end

rspec

# rspecの設定ファイルをインストール
rails generate rspec:install
# 不要なファイルが自動生成されない
config.generators do |g|
  g.test_framework :rspec,
                   view_specs: false,
                   helper_specs: false,
                   routing_specs: false
end
# RSpec 関連の設定ファイルを spec/support ディレクトリに配置する設定
# コメントアウトを外す
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

devise

# deviseの設定ファイルをインストール
rails generate devise:install
# 例)deviseでUserモデルを作成
rails generate devise User

deviseでユーザー登録を行う際に入力したメールアドレスにメールを送り、登録を完了させるという機能を追加できる。メールに記載するURLのドメイン名をあらかじめ設定しておく必要がある。

Rails.application.configure do
  # 以下を追記
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end

simple_form

# プロジェクトにbootstrapのアセットを適用したsimple_formを適用
rails generate simple_form:install --bootstrap

終わりに

忘れていることもあるかもしれないので、思い立ったら追記していこうと思います。

参考

gem

ブログ