Ruby on Rails integration

How to use Vue 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 Vue + Bootstrap 5 front-end.


Prerequisites

Before starting the project make sure to install the following utilities:

  • Node LTS (14.x.x or higher recommended)
  • Ruby (2.7.0 or higher recommended)
  • MySQL (8.x.x or higher recommended)
  • Yarn (1.22.19 or higher recommended) - for optional step only
  • Code editor. We recommend VSCode

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.

Step 1

Creating MySQL database.

In order to create a new database you need to run the following command:

  • Create a new user
  • Provide username, password, database name and description.

CLI will display your username, password, database name and connections string. 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.

Important: Do not close your terminal window until you save your credentials somewhere. This is the only time we will show you your database password. If you won't save it you'll loose it.

Step 2

Create a table.

Go to phpMyAdmin, log in, and add a table. For example on purpose of this tutoria with name tasks. Then create a three columns: id (type: int), name (type: text) and description (type: text).

Step 3

Install Rails by applying this command into terminal:

Step 4

Navigate to you root directory and create a new project with a rails command and additioanl flags: -d mysql and --api - this creates Ruby on Rails scaffold prepared only for working as a API for our frontend project & integrated with MySQL database. ruby-api is the name (you can change whatever you like).

Note: In case of error bound to install mysql2 gem, check solution from Github thread.

Step 5

Then go to newly created project.

Step 6

Create a controller and model. Task is name of your file - you can change whatever you like. After that you should have new files in app/controllers and app/models directories - in this particular case all of them will have the name Tasks (framework creates the plural on its own). You can use built in Ruby pre-made function: rails generate scaffold Task.

Step 7

Delete table schema.

Go to file located in config/db/migrate directory - after scaffold Ruby has generated random numbers in first part but name of the second should be created_tasks. Just delete this file.

Step 8

Install additional library for handling CORS - rack-cors.

Step 9

Go to config/initializers directory and find cors.rb file - uncomment code under link from "Read more".

Step 10

Then go to root directory and in Gemfile uncomment line with gem "rack-cors".

Step 11

In newly created controller (tasks_controller.rb) change placeholders in POST and PATCH/PUT endpoints: replace task_params with this code: name: params[:name], description: params[:description], id: params[:id].

Step 12

Open database.yml file (which can you find in config directory) and edit deafult database settings with your credentials.

Step 13

Start your server.

Note: In case of error bound to tzinfo-data library check solution from Stackoverflow thread.


Creating MDB Vue application

Note: Don't forget to go back to your root folder before next step. Folders ruby-api and mdb5-free-vue should be in the same directory.

Step 1

Create a new vite project with our MDB starter. Run the command below and select MDB5 Free Vue starter.

Your folder structure should look like this

  ruby/ ├── ruby-api └── mdb5-free-vue  

Step 2

Let's make some changes to the created vue app. First we need to install axios inside our mdb5-free-vue directory.

Remove style.css file (don't forget to delete it from main.ts file) and remove HelloWorld file from components directory.

Step 3

Let's create a .env file inside a mdb5-free-vue directory. We have to add VITE_ before the name of your variable, because it's the only way to expose them to Vite-processed code. Don't forget to change URL to the one you created earlier.

Step 4

Add new content to Home.vue file inside the views directory.

Since our starter database contains some sample models and routes, let's use them. We will create an application that will show a list of tasks. We also intend to create a functonality for adding new tasks, changing their content and removing them.

We have already prepared the code for you, so go ahead and copy and paste it into App.vue.

Ok, so what's actually going on there. We use MDB components, MDBBtn, MDBModal, MDBListGroup, MDBInputs and few other. The modal will be responsible to show inputs that will allow you to add, edit and send tasks to the database. The Manage tasks button, gives possibilty to modify or remove tasks. At the end, the list group will display our data.

If you haven't done it already, run npm start in your terminal. The app should be fully functional and should work correctly with backend.


Creating a new Ruby on Rails application with JavaScript inside

Let's create a fresh Ruby on Rails application where JavaScript is already implemented so that we can go through all the steps together.

Step 1

Install Rails by applying this command into terminal:

Step 2

Navigate to you root directory and create a new project with a rails command. my_new_app is the name (you can change whatever you like).

Note: Despite of the fact that Ruby on Rails offers new tool - importmap - for easier and faster managing your JavaScript code (without additional module bundler), it is not working properly with Vue at the moment. More about importamp you can read in Ruby on Rails documentation.

Note: Following importmap issues, in this tutorial new Ruby on Rails application created with Vue is working on Webpack.

Step 3

Then go to newly created project.


Configuring a new Ruby on Rails application

Let's configure a fresh Ruby on Rails application so that we can go through all the steps together.

Note: The Ruby on Rails documentation makes it clear that it's better to use Yarn over NPM. So for the rest of this tutorial, we'll be using yarn add instead of npm install.

Step 1

Start by installing Webpack and Webpack CLI.

Step 2

Then install Webpack's dependencies - loaders for CSS files and Vue components (css-loader, style-loader and vue-loader).

Note: More about Webpack loaders can read on Webpack documentation.

Step 3

Sometimes in spite of fact that we started Ruby on Rails project with JavaScript and Webpack template, dependencies for them are not installing. So you got to do it by hand: you need a jsbundling-rails library.

Step 4

Edit Webpack config in webpack.config.js file by adding module object with rules for vue-loader, css-loader adn style-loader and initiate new VueLoaderPlugin() in plugins array. Remeber to import it!

Step 5

If you are using Windows, edit Procfile.dev by adding before bin/rails server -p 3000 commend ruby. Then it should looks like this:


Installing MDB Vue & configuring frontend

Step 1

Install MDB Vue library.

Step 2

Install Vue.

Step 3

Uncomment root route in routes.rb file (you can found it in config directory. You can change name of this by whatever you like - all you got to remember that creating controller after that should has identical name!

Step 4

Create a controller for root route. After this command you are going to create a new file in app/views/articles directory: index.html.erb.

Step 5

Edit newly created file in app/views/articles directory: index.html.erb by adding root div for Vue application.

Step 6

Create App.vue file and put it into app/javascript directory (you can change it but remember to assign new import paths).

Step 7

Edit application.ts file (found in app/javascript directory) by creating a Vue app instance and imporing MDB's CSS file - after this it should looks like this:

Step 8

Then edit application.html.erb file (found in app/views/layouts directory) by adding in head CDN links for Roboto and Awesome fonts.

Step 9

For start your application (both backend and frontend) use bin/dev command.

Note: In case of error bound to tzinfo-data library check solution from Stackoverflow thread.


Optimization

If you want to further optimize your application please visit: