How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (2024)

Introduction

If you are developing a Ruby on Rails application, you may be interested in adding styles to your project to facilitate user engagement. One way to do this is by adding Bootstrap, an HTML, CSS, and JavaScript framework designed to simplify the process of making web projects responsive and mobile ready. By implementing Bootstrap in a Rails project, you can integrate its layout conventions and components into your application to make user interactions with your site more engaging.

In this tutorial, you will add Bootstrap to an existing Rails project that uses the webpack bundler to serve its JavaScript and CSS assets. The goal will be to create a visually appealing site that users can interact with to share information about sharks:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (1)

Prerequisites

To follow this tutorial, you will need:

  • A local machine or development server running Ubuntu 18.04. Your development machine should have a non-root user with administrative privileges and a firewall configured with ufw. For instructions on how to set this up, see our Initial Server Setup with Ubuntu 18.04 tutorial.
  • Node.js and npm installed on your local machine or development server. This tutorial uses Node.js version 10.16.3 and npm version 6.9.0. For guidance on installing Node.js and npm on Ubuntu 18.04, follow the instructions in the “Installing Using a PPA” section of How To Install Node.js on Ubuntu 18.04.
  • Ruby, rbenv, and Rails installed on your local machine or development server, following Steps 1-4 in How To Install Ruby on Rails with rbenv on Ubuntu 18.04. This tutorial uses Ruby 2.5.1, rbenv 1.1.2, and Rails 5.2.3.
  • SQLite installed, following Step 1 of How To Build a Ruby on Rails Application. This tutorial uses SQLite 3 3.22.0.

Step 1 — Cloning the Project and Installing Dependencies

Our first step will be to clone the rails-stimulus repository from the DigitalOcean Community GitHub account. This repository includes the code from the setup described in How To Add Stimulus to a Ruby on Rails Application, which described how to add Stimulus.js to an existing Rails 5 project.

Clone the repository into a directory called rails-bootstrap:

  1. git clone https://github.com/do-community/rails-stimulus.git rails-bootstrap

Navigate to the rails-bootstrap directory:

  1. cd rails-bootstrap

In order to work with the project code, you will first need to install the project’s dependencies, which are listed in its Gemfile. Use the following command to install the required gems:

  1. bundle install

Next, you will install your Yarn dependencies. Because this Rails 5 project has been modified to serve assets with webpack, its JavaScript dependencies are now managed by Yarn. This means that it’s necessary to install and verify the dependencies listed in the project’s package.json file.

Run the following command to install these dependencies:

  1. yarn install --check-files

The --check-files flag checks to make sure that any files already installed in the node_modules directory have not been removed.

Next, run your database migrations:

  1. rails db:migrate

Once your migrations have finished, you can test the application to ensure that it is working as expected. Start your server with the following command if you are working locally:

  1. rails s

If you are working on a development server, you can start the application with:

  1. rails s --binding=your_server_ip

Navigate to localhost:3000 or http://your_server_ip:3000. You will see the following landing page:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (2)

To create a new shark, click on the New Shark link at the bottom of the page, which will take you to the sharks/new route. You will be prompted for a username (sammy) and password (shark), thanks to the project’s authentication settings. The new view looks like this:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (3)

To verify that the application is working, we can add some demo information to it. Input “Great White” into the Name field and “Scary” into the Facts field:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (4)

Click on the Create Shark button to create the shark:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (5)

You have now installed the necessary dependencies for your project and tested its functionality. Next, you can make a few changes to the Rails application so that users encounter a main landing page before navigating to the shark information application itself.

Step 2 — Adding a Main Landing Page and Controller

The current application sets the root view to the main shark information page, the index view for the sharks controller. While this works to get users to the main application, it may be less desirable if we decide to develop the application in the future and add other capabilities and features. We can reorganize the application to have the root view set to a home controller, which will include an index view. From there, we can link out to other parts of the application.

To create the home controller, you can use the rails generate command with the controller generator. In this case, we will specify that we want an index view for our main landing page:

  1. rails generate controller home index

With the controller created, you’ll need to modify the root view in the project’s config/routes.rb file — the file that specifies the application’s route declarations — since the root view is currently set to the sharks index view.

Open the file:

  1. nano config/routes.rb

Find the following line:

~/rails-bootstrap/config/routes.rb

. . . root 'sharks#index'. . .

Change it to the following:

~/rails-bootstrap/config/routes.rb

. . . root 'home#index'. . .

This will set the home controller’s index view as the root of the application, making it possible to branch off to other parts of the application from there.

Save and close the file when you are finished editing.

With these changes in place, you are ready to move on to adding Bootstrap to the application.

Step 3 — Installing Bootstrap and Adding Custom Styles

In this step, you will add Bootstrap to your project, along with the tool libraries that it requires to function properly. This will involve importing libraries and plugins into the application’s webpack entry point and environment files. It will also involve creating a custom style sheet in your application’s app/javascript directory, the directory where the project’s JavaScript assets live.

First, use yarn to install Bootstrap and its required dependencies:

  1. yarn add bootstrap jquery popper.js

Many of Bootstrap’s components require JQuery and Popper.js, along with Bootstrap’s own custom plugins, so this command will ensure that you have the libraries you need.

Next, open your main webpack configuration file, config/webpack/environment.js with nano or your favorite editor:

  1. nano config/webpack/environment.js

Inside the file, add the webpack library, along with a ProvidePlugin that tells Bootstrap how to interpret JQuery and Popper variables.

Add the following code to the file:

~/rails-bootstrap/config/webpack/environment.js

const { environment } = require('@rails/webpacker')const webpack = require("webpack") environment.plugins.append("Provide", new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', Popper: ['popper.js', 'default']})) module.exports = environment

The ProvidePlugin helps us avoid the multiple import or require statements we would normally use when working with JQuery or Popper modules. With this plugin in place, webpack will automatically load the correct modules and point the named variables to each module’s loaded exports.

Save and close the file when you are finished editing.

Next, open your main webpack entry point file, app/javascript/packs/application.js:

  1. nano app/javascript/packs/application.js

Inside the file, add the following import statements to import Bootstrap and the custom scss styles file that you will create next:

. . . [label ~/rails-bootstrap/app/javascript/packs/application.js]import { Application } from "stimulus"import { definitionsFromContext } from "stimulus/webpack-helpers"import "bootstrap"import "../stylesheets/application". . . 

Save and close the file when you are finished editing.

Next, create a stylesheets directory for your application style sheet:

  1. mkdir app/javascript/stylesheets

Open the custom styles file:

  1. nano app/javascript/stylesheets/application.scss

This is an scss file, which uses Sass instead of CSS. Sass, or Syntactically Awesome Style Sheets, is a CSS extension language that lets developers integrate programming logic and conventions like shared variables into styling rules.

In the file, add the following statements to import the custom Bootstrap scss styles and Google fonts for the project:

~/rails-bootstrap/app/javascript/stylesheets/application.scss

@import "~bootstrap/scss/bootstrap";@import url('https://fonts.googleapis.com/css?family=Merriweather:400,700');

Next, add the following custom variable definitions and styles for the application:

~/rails-bootstrap/app/javascript/stylesheets/application.scss

. . .$white: white;$black: black;.navbar { margin-bottom: 0; background: $black;}body { background: $black; color: $white; font-family: 'Merriweather', sans-serif;}h1,h2 { font-weight: bold;}p { font-size: 16px; color: $white;}a:visited { color: $black;}.jumbotron { background: #0048CD; color: $white; text-align: center; p { color: $white; font-size: 26px; }}.link { color: $white;}.btn-primary { color: $white; border-color: $white; margin-bottom: 5px;}.btn-sm { background-color: $white; display: inline-block;}img,video,audio { margin-top: 20px; max-width: 80%;}caption { float: left; clear: both;}

Save and close the file when you are finished editing.

You have added Bootstrap to your project, along with some custom styles. Now you can move on to integrating Bootstrap layout conventions and components into your application files.

Step 4 — Modifying the Application Layout

Our first step in integrating Bootstrap conventions and components into the project will be adding them to the main application layout file. This file sets the elements that will be included with each rendered view template for the application. In this file, we’ll make sure our webpack entry point is defined, while also adding references to a shared navigation headers partial and some logic that will allow us to render a layout for the views associated with the shark application.

First, open app/views/layouts/application.html.erb, your application’s main layout file:

  1. nano app/views/layouts/application.html.erb

Currently, the file looks like this:

~/rails-bootstrap/app/views/layouts/application.html.erb

<!DOCTYPE html><html> <head> <title>Sharkapp</title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <%= yield %> </body></html>

The code renders things like cross-site request forgery protection parameters and tokens for dynamic forms, a csp-nonce for per-session nonces that allows in-line script tags, and the application’s style sheets and javascript assets. Note that rather than having a javascript_link_tag, our code includes a javascript_pack_tag, which tells Rails to load our main webpack entry point at app/javascript/packs/application.js.

In the <body> of the page, a yield statement tells Rails to insert the content from a view. In this case, because our application root formerly mapped to the index shark view, this would have inserted the content from that view. Now, however, because we have changed the root view, this will insert content from the home controller’s index view.

This raises a couple of questions: Do we want the home view for the application to be the same as what users see when they view the shark application? And if we want these views to be somewhat different, how do we implement that?

The first step will be deciding what should be replicated across all application views. We can leave everything included under the <header> in place, since it is primarily tags and metadata that we want to be present on all application pages. Within this section, however, we can also add a few things that will customize all of our application views.

First, add the viewport meta tag that Bootstrap recommends for responsive behaviors:

~/rails-bootstrap/app/views/layouts/application.html.erb

<!DOCTYPE html><html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sharkapp</title> <%= csrf_meta_tags %> <%= csp_meta_tag %>. . .

Next, replace the existing title code with code that will render the application title in a more dynamic way:

~/rails-bootstrap/app/views/layouts/application.html.erb

<!DOCTYPE html><html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><%= content_for?(:title) ? yield(:title) : "About Sharks" %></title> <%= csrf_meta_tags %> <%= csp_meta_tag %>. . .

Add a <meta> tag to include a description of the site:

~/rails-bootstrap/app/views/layouts/application.html.erb

<!DOCTYPE html><html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><%= content_for?(:title) ? yield(:title) : "About Sharks" %></title> <meta name="description" content="<%= content_for?(:description) ? yield(:description) : "About Sharks" %>"> <%= csrf_meta_tags %> <%= csp_meta_tag %>. . .

With this code in place, you can add a navigation partial to the layout. Ideally, each of our application’s pages should include a navbar component at the top of the page, so that users can easily navigate from one part of the site to another.

Under the <body> tag, add a <header> tag and the following render statement:

~/rails-bootstrap/app/views/layouts/application.html.erb

 <body> <header> <%= render 'layouts/navigation' %> </header> <%= yield %>. . .

This <header> tag allows you to organize your page content, separating the navbar from the main page contents.

Finally, you can add a <main> element tag and some logic to control which view, and thus which layout, the application will render. This code uses the content_for method to reference a content identifier that we will associate with our sharks layout in the next step.

Replace the existing yield statement with the following content:

~/rails-bootstrap/app/views/layouts/application.html.erb

. . . <body> <header> <%= render 'layouts/navigation' %> </header> <main role="main"> <%= content_for?(:content) ? yield(:content) : yield %> </main> </body></html>

Now, if the :content block is set, the application will yield the associated layout. Otherwise, thanks to the ternary operator, it will do an implicit yield of the view associated with the home controller.

Once you have made these changes, save and close the file.

With the application-wide layout set, you can move on to creating the shared navbar partial and the sharks layout for your shark views.

In addition to the changes you made to the application layout in the previous Step, you will want to create the shared navbar partial, the sharks layout that you referenced in app/views/layouts/application.html.erb, and a view for the application landing page. You can also add Bootstrap styles to your application’s current link_to elements in order to take advantage of built-in Bootstrap styles.

First, open a file for the shared navbar partial:

nano app/views/layouts/_navigation.html.erb

Add the following code to the file to create the navbar:

~/rails-bootstrap/app/views/layouts/_navigation.html.erb

<nav class="navbar navbar-dark navbar-static-top navbar-expand-md"> <div class="container"> <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> </button> <%= link_to "Everything Sharks", root_path, class: 'navbar-brand' %> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav mr-auto"> <li class='nav-item'><%= link_to 'Home', home_index_path, class: 'nav-link' %></li> <li class='nav-item'><%= link_to 'Sharks', sharks_path, class: 'nav-link' %></li> </li> </ul> </div> </div></nav>

This navbar creates a link for the application root using the link_to method, which maps to the application root path. The navbar also includes two additional links: one to the Home path, which maps to the home controller’s index view, and another to the shark application path, which maps to the shark index view.

Save and close the file when you are finished editing.

Next, open a file in the layouts directory for the sharks layout:

  1. nano app/views/layouts/sharks.html.erb

Before adding layout features, we will need to ensure that the content of the layout is set as the :content block that we reference in the main application layout. Add the following lines to the file to create the block:

~/rails-bootstrap/app/views/layouts/sharks.html.erb

<% content_for :content do %><% end %>

The code we’re about to write in this block will be rendered inside the :content block in the app/views/layouts/application.html.erb file whenever a sharks view is requested by a controller.

Next, inside the block itself, add the following code to create a jumbotron component and two containers:

~/rails-bootstrap/app/views/layouts/sharks.html.erb

<% content_for :content do %> <div class="jumbotron text-center"> <h1>Shark Info</h1> </div> <div class="container"> <div class="row"> <div class="col-lg-6"> <p> <%= yield %> </p> </div> <div class="col-lg-6"> <p> <div class="caption">You can always count on some sharks to be friendly and welcoming!</div> <img src="https://assets.digitalocean.com/articles/docker_node_image/sammy.png" alt="Sammy the Shark"> </p> </div> </div> </div> <% end %>

The first container includes a yield statement that will insert the content from the shark controller’s views, while the second includes a reminder that certain sharks are always friendly and welcoming.

Finally, at the bottom of the file, add the following render statement to render the application layout:

~/rails-bootstrap/app/views/layouts/sharks.html.erb

. . . </div> </div> </div> <% end %> <%= render template: "layouts/application" %>

This sharks layout will provide the content for the named :content block in the main application layout; it will then render the application layout itself to ensure that our rendered application pages have everything we want at the application-wide level.

Save and close the file when you are finished editing.

We now have our partials and layouts in place, but we haven’t yet created the view that users will see when they navigate to the application home page, the home controller’s index view.

Open that file now:

  1. nano app/views/home/index.html.erb

The structure of this view will match the layout we defined for shark views, with a main jumbotron component and two containers. Replace the boilerplate code in the file with the following:

~/rails-bootstrap/app/views/home/index.html.erb

<div class="jumbotron"> <div class="container"> <h1>Want to Learn About Sharks?</h1> <p>Are you ready to learn about sharks?</p> <br> <p> <%= button_to 'Get Shark Info', sharks_path, :method => :get, :class => "btn btn-primary btn-lg"%> </p> </div></div><div class="container"> <div class="row"> <div class="col-lg-6"> <h3>Not all sharks are alike</h3> <p>Though some are dangerous, sharks generally do not attack humans. Out of the 500 species known to researchers, only 30 have been known to attack humans. </p> </div> <div class="col-lg-6"> <h3>Sharks are ancient</h3> <p>There is evidence to suggest that sharks lived up to 400 million years ago. </p> </div> </div></div>

Now, when landing on the home page of the application, users will have a clear way to navigate to the shark section of the application, by clicking on the Get Shark Info button. This button points to the shark_path — the helper that maps to the routes associated with the sharks controller.

Save and close the file when you are finished editing.

Our last task will be to transform some of the link_to methods in our application into buttons that we can style using Bootstrap. We will also add a way to navigate back to the home page from the sharks index view.

Open the sharks index view to start:

  1. nano app/views/sharks/index.html.erb

At the bottom of the file, locate the link_to method that directs to the new shark view:

~/rails-bootstrap/app/views/sharks/index.html.erb

. . .<%= link_to 'New Shark', new_shark_path %>

Modify the code to turn this link into a button that uses Bootstrap’s "btn btn-primary btn-sm" class:

~/rails-bootstrap/app/views/sharks/index.html.erb

. . .<%= link_to 'New Shark', new_shark_path, :class => "btn btn-primary btn-sm" %>

Next, add a link to the application home page:

~/rails-bootstrap/app/views/sharks/index.html.erb

. . .<%= link_to 'New Shark', new_shark_path, :class => "btn btn-primary btn-sm" %> <%= link_to 'Home', home_index_path, :class => "btn btn-primary btn-sm" %>

Save and close the file when you are finished editing.

Next, open the new view:

  1. nano app/views/sharks/new.html.erb

Add the button styles to the link_to method at the bottom of the file:

~/rails-bootstrap/app/views/sharks/new.html.erb

. . . <%= link_to 'Back', sharks_path, :class => "btn btn-primary btn-sm" %>

Save and close the file.

Open the edit view:

  1. nano app/views/sharks/edit.html.erb

Currently, the link_to methods are arranged like this:

~/rails-bootstrap/app/views/sharks/edit.html.erb

. . . <%= link_to 'Show', @shark %> |<%= link_to 'Back', sharks_path %>

Change their arrangement on the page and add the button styles, so that the code looks like this:

~/rails-bootstrap/app/views/sharks/edit.html.erb

. . . <%= link_to 'Show', @shark, :class => "btn btn-primary btn-sm" %> <%= link_to 'Back', sharks_path, :class => "btn btn-primary btn-sm" %>

Save and close the file.

Finally, open the show view:

  1. nano app/views/sharks/show.html.erb

Find the following link_to methods:

~/rails-bootstrap/app/views/sharks/show.html.erb

. . . <%= link_to 'Edit', edit_shark_path(@shark) %> |<%= link_to 'Back', sharks_path %>. . . 

Change them to look like this:

~/rails-bootstrap/app/views/sharks/show.html.erb

. . . <%= link_to 'Edit', edit_shark_path(@shark), :class => "btn btn-primary btn-sm" %> <%= link_to 'Back', sharks_path, :class => "btn btn-primary btn-sm" %>. . .

Save and close the file.

You are now ready to test the application.

Start your server with the appropriate command:

  • rails s if you are working locally
  • rails s --binding=your_server_ip if you are working with a development server

Navigate to localhost:3000 or http://your_server_ip:3000, depending on whether you are working locally or on a server. You will see the following landing page:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (6)

Click on Get Shark Info. You will see the following page:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (7)

You can now edit your shark, or add facts and posts, using the methods described in How To Add Stimulus to a Ruby on Rails Application. You can also add new sharks to the conversation.

As you navigate to other shark views, you will see that the shark layout is always included:

How To Add Bootstrap to a Ruby on Rails Application | DigitalOcean (8)

You now have Bootstrap integrated into your Rails application. From here, you can move forward by adding new styles and components to your application to make it more appealing to users.

Conclusion

You now have Bootstrap integrated into your Rails application, which will allow you to create responsive and visually appealing styles to enhance your users’ experience of the project.

To learn more about Bootstrap features and what they offer, please see the Bootstrap documentation. You can also look at the documentation for Sass, to get a sense of how you can use it to enhance and extend your CSS styles and logic.

If you are interested in seeing how Bootstrap integrates with other frameworks, please see How To Build a Weather App with Angular, Bootstrap, and the APIXU API. You can also learn about how it integrates with Rails and React by reading How To Set Up a Ruby on Rails Project with a React Frontend.

How To Add Bootstrap to a Ruby on Rails Application  | DigitalOcean (2024)

FAQs

How to add bootstrap in ruby on rails? ›

Adding Bootstrap 5 to Your Rails 6 Application: A Step-by-Step...
  1. Step 1: Create a New Rails Application. ...
  2. Step 2: Add the Bootstrap Gem. ...
  3. Step 3: Import Bootstrap Stylesheets and JavaScript. ...
  4. Step 4: Generate a Custom SCSS File. ...
  5. Step 5: Customize Bootstrap. ...
  6. Step 6: Include Bootstrap in Your Layout.
Jul 6, 2023

How to integrate Bootstrap theme in Rails? ›

How To Add Bootstrap to a Ruby on Rails Application
  1. Prerequisites.
  2. Step 1 — Cloning the Project and Installing Dependencies.
  3. Step 2 — Adding a Main Landing Page and Controller.
  4. Step 3 — Installing Bootstrap and Adding Custom Styles.
  5. Step 4 — Modifying the Application Layout.
Nov 6, 2019

How to use Bootstrap icons in Rails? ›

Using bootstrap svg icons in Rails
  1. Add this to your Gemfile. $ gem 'bootstrap-icons-helper'
  2. Use this tag in your erbs. $ <%= bootstrap_icon "check", width: 5, height: 5, fill: "blue" %>

How to add CDN in Rails? ›

Here's how we set up Cloudflare as a CDN for our Rails applications.
  1. Configure CNAME record for CDN subdomain in Cloudflare. We need a subdomain that will act as a CDN. ...
  2. Turn on the Proxy feature in Cloudflare. ...
  3. Set appropriate Cache-Control headers in Rails. ...
  4. Configure asset_host in Rails. ...
  5. Verify changes.
Dec 18, 2023

How to add a Bootstrap? ›

Quick start
  1. Create a new index.html file in your project root. Include the <meta name="viewport"> tag as well for proper responsive behavior in mobile devices. <! ...
  2. Hello, world! Open the page in your browser of choice to see your Bootstrapped page.

How to add custom Bootstrap? ›

If you're a beginner, you can customize Bootstrap with a custom CSS stylesheet. CSS specificity is important here. You write custom CSS, with the same or higher specificity, and link to it in the head section of your index. html after the line which links to the original Bootstrap CSS.

How do I link Bootstrap to my project? ›

To add Bootstrap to an existing project, you can add the required link tag to your head element and the script tags right before the closing </body> tag. After adding Bootstrap, you should see some of the styles on your web page change.

How to use Bootstrap modal in Rails? ›

Creating Rails Modals Using Bootstrap
  1. Step 1: Initial Setup. Add gem 'bootstrap' and gem 'popperjs' to your Gemfile and run bundle install . ...
  2. Step 2: Modify Layouts in View. Make sure that this view is partial. ...
  3. Step 3: Modify Your Controller. ...
  4. Step 4: Enjoy!

How to install a theme in Bootstrap? ›

Install a theme's dependencies from your command line via npm. (i.e. npm install ) Run a "watch" or "build" command powered by Gulp, Grunt, or Webpack build tools. (i.e. npm start or gulp )

Where do I put Bootstrap Icons? ›

Include the Bootstrap Icons font stylesheet in the <head> of your website. Or, use @import to include the stylesheet that way. /* Option 2: Import via CSS */ @import url("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css"); Add HTML snippets to include Bootstrap Icons where desired.

How can you display code in bootstrap? ›

Bootstrap provides a number of classes for displaying inline and multiline blocks of code. Displaying Inline Code: Inline code should be wrapped in <code> tags. The resulting text will be displayed in a fixed-width font and given a red font color.

How to use Bootstrap Datepicker in Rails? ›

Bootstrap Datepicker for Rails
  1. gem 'bootstrap-datepicker-rails'
  2. gem 'bootstrap-datepicker-rails', :require => 'bootstrap-datepicker-rails', :git => 'git://github.com/Nerian/bootstrap-datepicker-rails.git'
  3. *= require bootstrap-datepicker # Or if using bootstrap v3: *= require bootstrap-datepicker3.

How to add Bootstrap CDN in rails? ›

How to add Bootstrap 4.4. 1 to Rails
  1. Quick Install (CDN) Add in your <head></head> the following tags: <!-- ...
  2. Add Bootstrap 4 in the Rails asset pipeline. To do that we need to add bootstrap and jquery in your Gemfile: gem 'bootstrap', '~> 4.4.1' gem 'jquery-rails'

How to add custom CSS in rails 7? ›

How to add custom CSS beside Bootstrap with Rails 7
  1. Add new file stylesheets/custom. css and create new css class.
  2. Add import to application. bootstrap. ...
  3. yarn run build:css.
  4. Now I can see my-custom-class in builds/application. css alongside with bootstrap css.
  5. try to use this class in HTML, not working.
Feb 28, 2022

How to add API to rails? ›

A Guide on How to Build a Ruby on Rails API
  1. STEP 1: Create a new Rails API.
  2. STEP 2: Enable CORS (Cross Origin Resource Sharing)
  3. STEP 3: Create model, controller, database migration table and route via rails g resource command.
  4. STEP 4: Specify attributes and datatypes of a menu item.

How to add custom CSS in Ruby on Rails? ›

Using Plain CSS
  1. Create CSS File: Create a new file with the . css extension in the app/assets/stylesheets directory (e.g., custom. css).
  2. Write CSS Styles: Add your custom styles directly to the CSS file: custom.css. CSS. ...
  3. Link CSS File: Link the CSS file in your application layout using the stylesheet_link_tag helper:
May 2, 2024

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6557

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.