【Rails】俺好みのrails new備忘録
rails new して色々試すときにあれ?いつも何やるっけってことをなくすために、rails newのときによくすることを備忘録として残しました。
rails new
- データベース: postgresql
- JavaScriptライブラリー: esbuild
- CSSプロセッサ: bootstrap
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
- GitHub - haml/html2haml: Convert HTML and HTML+Erb to Haml.
- GitHub - rspec/rspec-rails: RSpec for Rails 5+
- GitHub - thoughtbot/factory_bot: A library for setting up Ruby objects as test data.
- GitHub - heartcombo/devise: Flexible authentication solution for Rails with Warden.
- GitHub - brainspec/enumerize: Enumerated attributes with I18n and ActiveRecord/Mongoid support
- GitHub - heartcombo/simple_form: Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.