As seen in previous post, I tried to install Rails 7 and Botstrap using command:
$rails new app-name -j esbuild --css bootstrap
The problem with this approach is
- It does not install importmap which is recommended default for Rails 7.
- It installed so many node_modules.
- I could not install Font Awesome because I did not have importmap.
So my thinking was to create a new Rails 7 app (so that Importmap will be default) and THEN ADD Bootstrap to it.
This approach is easier in my experience. It is also better in my opinion because it will have importmap as default.
Here is how to do that. This video was super helpful, but I am writing all steps below.
$rails new rails7importmap_add_bootstrap
$cd rails7importmap_add_bootstrap
Verify the app by running $rails s (Note: ./bin/dev does not work. There is no bin/dev file nor Procfile.dev either.)
$bin/importmap pin bootstrap
File app/javascript/application.js : Add import "bootstrap"
Add bootstrap to your Gemfile following instructions: gem 'bootstrap', '~> 5.2.2'
$bundle install
Rename app/assets/stylesheets/application.css to application.scss
Then add to application.scss : @import 'bootstrap';
$rails s #start server
Verify CSS part for Bootstrap is handled correctly. (The font changes)
Add navbar code from Bootstrap documentation to app/views/layouts/application.html.erb, but you will notice that menu dropdown does not work. That is because javascript part for Bootstrap is still not working correctly.
Modify config/import.rb as shown below. (This is very important step)
Change URL for @popperjs/core to
The code will look like shown below:
Now I want to install font awesome (using importmap).
$./bin/importmap pin @fortawesome/fontawesome-free #(Note that first keyword is fort and NOT font. I made that mistake).
This will add following to config/importmap.rb
Change fontawesome.js to all.js
Then add to app/javascript/application.js
Use code to display font awesome icons as shown in documentation
To add bootstrap icons, add the following to app/assets/stylesheets/application.scss
TODO:
- Add Popover and Toolstips for Bootstrap
Popover and tooltips not working in spite of adding following code to app/javascript/application.js