[RUBY] I put Bootstrap in Rails6. (Sprockets :: Rails :: Helper :: AssetNotPrecompiled handling)

Added to Gemfile

Gemfile


gem 'uglifier'
gem 'jquery-rails'
gem 'bootstrap'
bundle install

Make sure the JS file is compressed in the production environment.

/config/environments/production.rb


config.assets.js_compressor = :uglifier

Create a directory: javascripts under assets, create a new application.js and add it.

app/assets/javascripts/application.js


//= require jquery3
//= require popper
//= require bootstrap

Rename application.css under stylesheets to application.scss and add.

app/assets/stylesheets/application.scss


@import "bootstrap";

Load into layout

html:app/views/layouts/application.html.erb


<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

In my environment, I got an error saying Sprockets :: Rails :: Helper :: AssetNotPrecompiled, so I added it.

config/initializers/assets.rb


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

You have now confirmed that Bootstrap has been applied.

スクリーンショット 2020-11-15 12.49.07.png

Recommended Posts

I put Bootstrap in Rails6. (Sprockets :: Rails :: Helper :: AssetNotPrecompiled handling)
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Where I got stuck in today's "rails tutorial" (2020/10/08)
I tried to organize the session in Rails
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
Where I got stuck in today's "rails tutorial" (2020/10/04)
[Ruby on Rails] How to install Bootstrap in Rails
Where I got stuck in today's "rails tutorial" (2020/10/07)