Ruby on Rails Integration
How to use Bootstrap 5 with Ruby on Rails - free starter
This guide will provide you with a free template for a Ruby on Rails application, with MySQL database and Bootstrap 5 front-end.
Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Ruby on Rails application
Let's create a fresh Ruby on Rails application so that we can go through all the steps together. For this tutorial we'll be using MySQL database.
Step 1
Install Ruby on Rails on your computer.
Step 2
Navigate within the terminal to the directory where you want to create your application then run the code from the snippet below to create your app and change the directory.
Note: In case of an error bound to the installation of the mysql2 gem, check the solution from this Github thread.
Step 3
Create a controller, model, and database for handling endpoints. Task
is the name of your file - you can change
whatever you like. After that you should have new files in app/controllers
, app/models
and db/migrate
directory - in this particular case all of them will have the name Tasks (framework creates the plural on its own).
Step 3
Creating a MySQL database.
In order to create a new database you need to run the following command.
Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli
.
Now log in with your MDB account, type: mdb login
.
If you don't have account yet you can create one using mdb register
command.
- Create a new user
- Provide username, password, database name and description.
CLI will display your username, password, database name and connections string. You will need this data in the next step. Now you can go to phpMyAdmin where you will be able to handle the administration of the MySQL database.
Note: the password must contain at least one uppercase letter, one lowercase letter, one number, one special symbol and have minimum length of 8.
Step 4
In the database.yml
file go to the development
section and add the database connection details.
Change the values between brackets
for the ones you got after creating a new MySQL Database from our starter.
Step 6
Migrate a database.
Step 7
Run your server with a command from the snippet below.
This will boot Puma which is the default Ruby web server. To access your application via any browser visit http://localhost:3000/. There you should see the Rails default information page. When you want to stop the web server, press Ctrl+C in the terminal window where it's running.
Install MDB
In this section, we will add MDB styles and JavaScript files to the Ruby on Rails app.
Step 1
From the base project directory type the following command in the terminal.
It will change the directory to vendor
and initialize MDB CLI. From the list of starters select Standard
.
Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli
.
Now log in with your MDB account, type: mdb login
.
If you don't have account yet you can create one using mdb register
command.
Step 2
Add vendor
to assets path in config/initializers/assets.rb
file.
Step 3
In the application.css
file import CSS files.
Step 4
In the application.js
add those lines to import JS files.
To Do App with MDB
In this section, we will present the capabilities of Ruby on Rails used with the MDB package by creating an example To Do App.
Step 1
From ./app/views/tasks
remove unnecessary files leaving _form.html.erb
, _task.html.erb
and index.html.erb
.
Step 2
We are going to modify default GET, POST, PUT/PATCH
and DELETE
endpoints that support the
functionality of the front-end app. To do that go to tasks_controller.rb
and replace its content with code from
the snippet below.
Step 3
At the end of the head
tag in the application.html.erb
file add the content of the snippet below.
Step 4
At the end of the body
tag in the application.html.erb
file add the content of the snippet below.
Step 5
In ./app/views/tasks
prepare partials _form.html.erb
and _task.html.erb
by replacing its current content with the content of the snippets below.
Step 6
In ./app/views/tasks/index.html.erb
paste the content of the snippet below.
To access your application visit http://localhost:3000/tasks. There you should see a simple To Do app connected to MySQL database.
Important: For Windows users, some more steps need to be taken. Please follow the steps below.
Additional step 1
In the package.json
change scripts
to match the content of the snippet below.
Additional step 2
From toDoApp
directory run the watch
script to watch changes in your JavaScript and CSS files.
Additional step 3
Restart your server to see changes.
Optimization
If you want to further optimize your application please visit:
Backend features
Ruby on Rails:
This example was created with the use of Ruby on Rails. Our app is connected to MySQL database and is ready to receive get, post, put
and delete
requests.
MDB CLI:
The database in this example was created using MDB CLI with Graphical User Interface available under phpmyadmin.mdbgo.com
Frontend features
MDB UI KIT:
We have successfully added MDB UI Kit to the created app, with which we can build basic views very quickly.
Views and Layouts:
We have successfully integrated the backend with the MDB package and can send basic requests to backend Ruby on Rails application.