Skip to content

Instantly share code, notes, and snippets.

@secretpray
Last active October 31, 2022 18:20
Show Gist options
  • Save secretpray/85a526583c763fd8a399c464820740de to your computer and use it in GitHub Desktop.
Save secretpray/85a526583c763fd8a399c464820740de to your computer and use it in GitHub Desktop.
Add bootstrap to Rails 7 with cssbundling

Add bootstrap to Rails 7 with cssbundling

ruby -v

-> 3+

rails -v

-> 7+

rails new app_name -d postgresql -T
cd app_name
./bin/bundle add cssbundling-rails
./bin/importmap pin bootstrap --from jsdelivr
./bin/rails css:install:bootstrap

optionaly (for development only)

bundle add faker --group "development"

Add to package.json

"scripts": {
  "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
}

Add customized_bootstrap.scss (not necessary section)

touch app/assets/stylesheets/customized_bootstrap.scss

Import customized_bootstrap.scss (prepend)

echo "@import 'customized_bootstrap';" | cat - app/assets/stylesheets/application.bootstrap.scss | tee app/assets/stylesheets/application.bootstrap.scss

Start application

./bin/rails g controller home index
./bin/rails db:drop db:create db:migrate db:seed
./bin/dev
@MtnBiker
Copy link

Hi. Ran across your trials here.

I'm wondering if you know how to add .css from a node_module? For example: node_modules/leaflet/dist/leaflet.css. Could copy into app/assets/stylesheets/ but seems like there should be a more robust way. If the node_module changes the css would not be current.

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