0
0

More than 3 years have passed since last update.

RailsでBootstrapのdropdownが使えない

Last updated at Posted at 2020-05-04

開発環境

Rails 6.0.2
ubuntu(WSL)
Bootstrap 4

前提として、cssファイルは読み込まれているものとします。

想定エラー

クリックしてもメニューがでてこない。
ターミナルにエラーは出てこない状態。

原因

  • ブラウザにjavascriptが読み込まれていない
  • HTMLのコードが間違っている

今回は上の場合を想定しています。

解決策

jqueryを使えるようにする

Gemfileに以下を追加します。
追加後にターミナルにbundleを入力してください。

gem 'jquery-rails'

ブラウザにjavascriptを読み込ませる

application.html.slim
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'

プリコンパイルするファイルを設定

config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( application.js )

マニフェストファイルの記述

app/assets/javascripts/application.js
//= require jquery
//= require rails-ujs
//= require popper
//= require turbolinks
//= require_tree .
//= require bootstrap
//= require activestorage

上記のこと全て行っても、以下のエラーが出た場合

ActionView::Template::Error (Asset `application.js` w
as not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/mash
nifest.js`.

ターミナルにrake assets:precompileを入力したら成功すると思います。

参考文献

0
0
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
0
0