Skip to content

Instantly share code, notes, and snippets.

@rubyandcoffee
Last active September 11, 2024 21:46
Show Gist options
  • Save rubyandcoffee/592cdf13586548a7a26c30adeeb7adc2 to your computer and use it in GitHub Desktop.
Save rubyandcoffee/592cdf13586548a7a26c30adeeb7adc2 to your computer and use it in GitHub Desktop.
Bootstrap with Rails 7
Adding Bootstrap to Rails 7
Reference: https://www.linkedin.com/pulse/rails-7-bootstrap-52-importmap-md-habibur-rahman-habib/
INSTRUCTIONS
1. Add the following to Gemfile:
gem "bootstrap"
gem "sassc-rails"
Then $ bundle install
2. Rename application.css to application.scss
3. Import Bootstrap styles in app/assets/stylesheets/application.scss:
@import "bootstrap";
4. Include bootstrap in your app/javascript/application.js:
import "popper"
import "bootstrap"
5. Go to config/importmap.rb and add the following:
pin "popper", to: 'popper.js', preload: true
pin "bootstrap", to: 'bootstrap.min.js', preload: true
6. Add the following to config/initializers/assets.rb:
Rails.application.config.assets.precompile += %w( bootstrap.min.js popper.js)
7. Test that everything works by adding some navigation (see below example) and restarting the server.
```
<div class="container-flouid">
<nav class="navbar navbar-expand-lg bg-light">
<div class="container">
<a class="navbar-brand" href="/">
<h3>Your Logo</h3>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Member</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Courses</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Event</a>
</li>
</ul>
<form class="">
<a class="btn btn-outline-primary mr-2" href="#">Login</a>
<a class="btn btn-outline-success" href="#">Become member</a>
</form>
</div>
</div>
</nav>
</div>
```
@AbdullahAlNaim
Copy link

Finally something that works thank you so much!

@AlexandreMdeAlmeida
Copy link

Thank you very much! If you don't specify the Bootstrap version, will it install the latest version directly?

@rubyandcoffee
Copy link
Author

@AlexandreMdeAlmeida You're very welcome and yes, it'll install the latest version 👍

@GregBaugues
Copy link

Thank you!

@AlexandreMdeAlmeida
Copy link

@AlexandreMdeAlmeida You're very welcome and yes, it'll install the latest version 👍

Thank you! 🙌🏼

@hallison
Copy link

Thanks!!! It works perfectly.

@master-coder-ayush
Copy link

Thank you so much!

@sevilla
Copy link

sevilla commented Jun 6, 2024

Thank you!

@Wellyson-Lopes
Copy link

Simple and perfect, I was looking for this for a long time, thank you
#Brasil

@kda-developer
Copy link

It works! A thousand thanks!

@dc20433
Copy link

dc20433 commented Jun 21, 2024

Many people claimed that it would work by simply starting a new rails project with --css=bootstrap -j esbuild. It consistently yields error messages such as "application cannot be found in the asset pipeline". It amazes me that something works so perfectly in such a straightforward way! Thank you!

@inmydelorean
Copy link

@dc20433 They claimed that because it works. You just need to start your Rails server with .bin/dev, not rails s to compile the assets. This guide is for importmaps.

@dc20433
Copy link

dc20433 commented Jun 25, 2024

Hi inmydelorean: I did $rails new testapp --css-bootstrap -j esbuild, and started the server in the project folder with bin/dev (.bin/dev shows No such file or directory), and got rails error messages: The asset "application.css" is not present in the asset pipeline, with the following line highlighted:

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

@Dutth77
Copy link

Dutth77 commented Jul 16, 2024

You can start the Rails server with bin/rails this should solve your problem.

bin/rails server -p 3000

@dc20433
Copy link

dc20433 commented Jul 17, 2024

Many thanks Dutth77. Using your method to start the Rails can get you the Rails logo page, but the error message I posted earlier will appear after creating Controller, Views, and Routes. I am using Rails 7.1.3.4 and Ruby 3.2.2.

@Brian-Momata
Copy link

Thank you! So if I want to write some custom CSS, which file will I write to?

@Brian-Momata
Copy link

Thank you! So if I want to write some custom CSS, which file will I write to?

Nevermind

@BitSlayer18
Copy link

My CSS is working fine but the javascript part doesn't looks good like it's not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment