OOZOU
お問い合わせ
TIL一覧に戻る
ruby on rails

Rails 6、Ruby 3、Bootstrap 5 と Hotwire

2021年4月18日

Bootrails のブログ記事を Hotwire で拡張します:
この TIL を書いている時点での最新のあれこれ:

$ ruby --version
3.0.1
$ rails --version
Rails 6.1.3.1
$ rails new MyApp --skip-bundle --skip-coffee --database=postgresql
$ cd MyApp
# Add Hotwire-rails to Gemfile
$ echo "\ngem 'hotwire-rails'" >> Gemfile
$ bundle
$ ./bin/rails webpacker:install
# lets keep javascript and stylesheets in a app/frontend folder
$ mv app/javascript app/frontend
# add bootstrap dependencies
$ yarn add bootstrap@next
# when prompted, choose the latest version here:
$ yarn add @popperjs/core@next

./webpacker.yml を編集して source_path を変更します:

# Inside webpacker.yml
default: &default
  source_path: app/frontend # Change here

bootstrap を読み込むファイルを追加します:

$ mkdir app/frontend/js && touch app/frontend/js/bootstrap_js_files.js
$ touch app/frontend/packs/application.scss
# Warning, this removes application.css (which we replace with application.scss)
$ touch app/assets/stylesheets/.keep && rm app/assets/stylesheets/application.css
// inside app/frontend/js/bootstrap_js_files.js
// enable plugins as you require them  

// import 'bootstrap/js/src/alert'  
// import 'bootstrap/js/src/button'  
// import 'bootstrap/js/src/carousel'  
import 'bootstrap/js/src/collapse'  
import 'bootstrap/js/src/dropdown'  
// import 'bootstrap/js/src/modal'  
// import 'bootstrap/js/src/popover'  
import 'bootstrap/js/src/scrollspy'  
// import 'bootstrap/js/src/tab'  
// import 'bootstrap/js/src/toast'  
// import 'bootstrap/js/src/tooltip'
// inside app/frontend/packs/application.js  

// Add this line before import Rails …
import '../js/bootstrap_js_files.js'  
// inside app/frontend/packs/application.scss  

// Import Bootstrap v5  
@import "~bootstrap/scss/bootstrap";

Hotwire をセットアップします

# create ./bin/bundle; currently required for the Remove Turbolinks task by Stimulus
# see: https://github.com/hotwired/stimulus-rails/issues/55
$ bundle binstubs bundler
$ ./bin/rails hotwire:install

app/views/layouts/application.html.erb を変更します:

<!DOCTYPE html>
<html>
  <head>
    <title>MyApp</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <!-- Warning !! ensure that "stylesheet_pack_tag" is used, line below; also change turbolinks to turbo for hotwire -->
    <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbo-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

すべてコンパイルできるか確認します:

$ ./bin/rails assets:clobber
# You can probably ignore the warnings :)
$ ./bin/rails webpacker:compile

「ruby on rails」の関連記事

ruby on rails2022年2月10日

Punditポリシーで許可パラメータをデフォルトとアクション別に管理する

許可パラメータを管理するには、ポリシーに permitted\attributes と permitted\attributes\for\#{action} を追加します。例:

ruby on rails2022年1月31日

Turboでのフォーム送信には `requestSubmit` を使う

Turboはformのsubmitイベントをリッスンします。そのため、JSでフォームを送信するときは必ず requestSubmit を使いましょう。

ruby on rails2022年1月19日

GitHub Actions で Rubocop と ESLint にバンドルされた gem を無視させる

Rails プロジェクトでは、GitHub Actions で gem を vendor ディレクトリにインストールして、以降の実行でキャッシュできるようにすることがよくあります。

プロジェクトをお考えですか?

あなたが構築しているものについてお聞かせください。

会話を始めるhello@oozou.com
OOZOU Logo

バンコク · シンガポール · 香港

X
Facebook
Instagram
LinkedIn

サービス

  • Web開発
  • AIエージェント & 生成AI
  • モバイルアプリ開発
  • データ分析 & エンジニアリング
  • UI/UX & プロダクトデザイン
  • デジタルトランスフォーメーション

会社情報

  • 会社概要
  • 採用情報
  • ブログ
  • 事例研究
  • Today I Learned
  • お問い合わせ

その他

  • 業界
  • パートナーネットワーク
© 2026 OOZOU. 無断転載を禁じます。
プライバシーポリシー行動規範ABACポリシー