Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stevepolitodesign/rails-6-bootstrap-4-integration
Integrate Rails 6 and Bootstrap 4 Using Webpacker. Inspired by GoRails.
https://github.com/stevepolitodesign/rails-6-bootstrap-4-integration
Last synced: about 17 hours ago
JSON representation
Integrate Rails 6 and Bootstrap 4 Using Webpacker. Inspired by GoRails.
- Host: GitHub
- URL: https://github.com/stevepolitodesign/rails-6-bootstrap-4-integration
- Owner: stevepolitodesign
- Created: 2020-07-30T10:33:16.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-19T20:15:09.000Z (over 1 year ago)
- Last Synced: 2023-03-06T00:21:56.615Z (over 1 year ago)
- Language: Ruby
- Homepage: http://stevepolito.design/blog/rails-6-bootstrap-4-webpacker-tutorial/
- Size: 1.34 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
I know many existing tutorials explain how to install Bootstrap 4 on a Rails 6 application with Webpacker (like the [GoRails example](https://gorails.com/episodes/how-to-use-bootstrap-with-webpack-and-rails)). However, I wanted to create this tutorial to highlight a few gotchas, as well as highlight _why_ and _how_ I made some of my choices.
1. `rails new rails-bootstrap`
2. `yarn add bootstrap jquery popper.js`
3. Add jQuery and Popper.js plugins. These are [required](https://getbootstrap.com/docs/4.5/getting-started/introduction/#js) by Bootstrap.> This step was always so cryptic to me. I would just blindly copy and paste what I saw on other tutorials, but I wanted to know _how_ people knew to even do this.
I found [this example](https://github.com/rails/webpacker/blob/749675d9c035dbc8777f718582e3e4804147e9e5/docs/webpack.md#plugins) in the Webpacker docs, so decided to use it knowing it was in the official documentation.
```js
// config/webpack/environment.js
const { environment } = require('@rails/webpacker')
const webpack = require('webpack')environment.plugins.prepend(
'Provide',
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
jquery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
})
)module.exports = environment
```
> Another big problem I ran into was how to correctly import Bootstrap's styles.I initially created `app/javascript/packs/application.scss` and imported the styles in that file. However, that seemed to break my build in a way that made it so `app/javascript/packs/application.js` never compiled.
Next, I renamed `app/assets/stylesheets/application.css` to `app/assets/stylesheets/application.scss`, and imported the styles into that file. That worked, but it meant that the Asset Pipeline was responsible for my styles. This isn't necessarily a bad thing, but I wanted Webpacker to be responsible for all of my front-end code.
Also, by using a `.scss` file, you can easily [override Bootstrap's default variales](https://getbootstrap.com/docs/4.5/getting-started/theming/#variable-defaults)
4. `mkdir app/javascript/`
5. `touch app/javascript/stylesheets/application.scss`
6. Import Bootstrap Styles```scss
// app/javascript/stylesheets/application.scss
@import '~bootstrap/scss/bootstrap'
```7. Import Bootstrap, load styles, and optionally load [Tooltips](https://getbootstrap.com/docs/4.5/components/tooltips/#example-enable-tooltips-everywhere) and [Popovers](https://getbootstrap.com/docs/4.5/components/popovers/#example-enable-popovers-everywhere) everywhere.
```js
// app/javascript/packs/application.js
...
require("bootstrap")
import "../stylesheets/application";
document.addEventListener("turbolinks:load", function() {
$(function () {
$('[data-toggle="tooltip"]').tooltip()
$('[data-toggle="popover"]').popover()
})
})
```> Webpacker emits css files only if `extract_css` is set to `true` in `webpacker.yml` otherwise `stylesheet_pack_tag` returns nil.
When I was running through these steps, I found it strange that I didn't need to add a [stylesheet_pack_tag](https://github.com/rails/webpacker/blob/bf278f9787704ed0f78038ad7d36c008abc2edfd/docs/css.md#link-styles-from-your-rails-views) as I had seen in other tutorials. I realized that this is because I was in development.
If you change `extract_css: false` to `extract_css: true` under the `default:` block in `config/webpacker.yml` and then restart your server, you'll notice that the styles no longer load.
8. To fix this, simply add the `stylesheet_pack_tag` and restart the server. After ensuring the styles haves loaded set `extract_css: true` back to `extract_css: false` under the `default:` block in `config/webpacker.yml`. You might need to run `rails webpacker:clobber` after making that change.
```erb{10}
<%# app/views/layouts/application.html.erb %>
RailsBootstrap4Integration
<%= csrf_meta_tags %>
<%= csp_meta_tag %><%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= yield %>
```
9. Finally, add the [responsive meta tag](https://getbootstrap.com/docs/4.5/getting-started/introduction/#responsive-meta-tag)
```erb{8}
<%# app/views/layouts/application.html.erb %>
RailsBootstrap4Integration
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= yield %>
```