Setting up Rails application with ReactJS usually takes some time.
Setting up React Router
and
Redux
for Front-end is an essential part
of
using React with Ruby on Rails.
This tutorial will help you
setup Ruby on Rails application
with
ReactJS.
It will also demonstrate how
to
setup React Routing
and
React components to perform basic Create(C), Read(R), Update(U)
and
Delete(D) operation.
If you are someone,
who works with multiple projects
and
create new projects every now and then,
it is troubling to spend time
on
setting up Ruby on Rails application
with
ReactJS.
Step 1: Install Ruby and Rails versions
To setup a new Rails 6 application,
you need to be on
Ruby 2.5.0+.
To install Ruby on Rails with RVM,
use the command given below.
We will use current stable ruby version
2.6.5
Once Ruby 2.6.5 is installed,
install gem for
rails 6.0.0
as given below.
Verify that you have correct Rails version installed
with
the command given below.
Step 2: Create a new Rails project
Create a new Rails project
with
the command given below.
This will create a new Rails 6 application
and
install necessary gems required
for
Rails 6 application.
--database option helps select database to use. We will use postgresql as database
and
--webpack option is used to select javascript framework. We will use react.
Webpack react makes sure package.json has
React
dependencies added.
Change directory to newly created Rails application directory.
Run database setup Rails command
to
get the repository
to
work with backend.
Let’s commit this repository
in
VCS (Git)
as initial repository code.
Perform bundle install to install gem as given below.
Once this is done,
we need to run react on rails initializers as given below.
Perform bundle install after step above,
since it adds gem mini_racer to Gemfile
and
React files for Hello World setup.
Step 4: Run Rails server: Hello World
React on Rails comes with Procfile.dev and Procfile.dev-server file.
Thus, we need to install
foreman gem
to
run multiple processes.
In this case,
there are two processes.
Rails Server
Webpack server
Procfile.dev-server is given below for the reference.
Run the server to view hello world component
built in React.
The above command will install
react-router
and
react-router-dom package
and
add it to package.json file.
This will also lock dependencies
in yarn.lock file.
Now, let’s create another
rails route
to
demonstrate react routing.
We will also add a root route that
points to same hello_world#index
controller action as given below.
Entire config/routes.rb file looks as given below.
We have three routes in total.
Root Route - Points to hello_world#index controller action
Hello World Route - Points to hello_world#index controller action
Bye World Route - Points to hello_world#index controller action
As we can see,
all there are two routes pointing
to
same controller#action.
This is delebrately done
to
show how react routing works.
Now, let’s add react route links.
Create a file App.js and Routes.js in app/javascript directory.
Let’s add a couple of routes to front-end
as
described in backend config/routes.rb file.
Let’s mount these routes under Router as given below
in App.js.
Restart the foreman server to see React Routing in action.
We have three routes in total.
Root Path - It shows list of routes as unordered list
Hello World Route Path - It shows Hello World Component
Bye World Route Path - It shows Bye World Component
Step 6: CRUD operation - Rails APIs
Now, we will demonstrate Create (C), Read (R), Update(U)
and
Delete (D) operations.
Let’s create an
ActiveRecord modelPost to begin with.
Run database migration and seed rake scripts
to
create posts database
and
populate seed data respectively.
Now that we have model and records for posts in database,
let’s define
routes
and
controller
to perform CRUD operation
on
posts entity.
Let’s add routes to access Create, Read, Update
and
Delete operations externally.
We have used resource
to
create routes
and
have specified exact four routes
that
we want via only option.
Please find why we have used
resource and not resources
To make sure,
we have routes correctly set up
and
they return response
for
posts resource,
let’s send a
cURL Request
to fetch a list of posts.
This command returns reponse as given below.
We are ready with backend APIs.
Let’s create front-end components
to
use these backend APIs
and
perform CRUD operation.
Step 7: CRUD operation - React components
First, we will create a component
to
list down all the available posts.
Create a new directory under
app/javascript/bundles
with
name posts
to create React files for posts entity.
Component to list Posts
This component does following things:
Initializes posts to an empty array in the constructor
Fetches posts to list from backend API that created in step above.
Render method iterates over posts from React state and displays them in a table format.
Let’s change React route to open Posts listing component
at
the root path.
Component to view a Post
We have a list of Posts displayed
at
the root path of the application.
Let us create a new React component
to
display an individual post.
This component does following things.
Initialize post as an empty object in a constructor
Fetch the post in question in componentDidMount lifecycle event based on post ID received in props
Render method returns HTML that prints title, description and is_published values
Create a new Route
for the post details page as given below
in config/routes.rb for backend
and
in Routes.js for React front-end.
Entire config/routes.rb looks as given below.
We have removed hello_world and bye_world routes now.
For react routes, add in app/javascript/Routes.js.
Entire routes files looks as given below.
We have removed hello_world and bye_world routes now.
Component to delete a post
To perform put, patch, delete requests
from API,
we need to add following code to app/controllers/application_controller.rb
Let’s add a link to delete a post
from
Posts list page. We will add two things.
Extra column in a table to show delete action
handleDelete callback that will send an ajax request to delete a record
The posts list page component is given below after the change mentioned above.
This component does things mentioned below.
Adds a handleDelete callback which send delete post ajax request