Daniel Troyano
Posted on September 26, 2020
In this series I'm going to walk through setting up a very basic Rails server. Then we're going to turn it into a basic CRUD API that returns JSON. And then finally we're going to setup a React.js frontend for our API.
Alright, let's hop into setting up our basic Rails server!
So, I'm going to assume you have Ruby installed already. If you don't I would recommend installing Ruby Version Manager. Also make sure you have Rails and SQLite installed, since we'll be using that later. If you don't, check out Install Rails.
Ok, let's begin for real now.
Rails has a lot of handy command line tools that we're going to be using, we'll go over them as they come up, and here we're going to use rails new dog_api
to create a new Rails application. dog_api
can stand in for whatever you want your application to be called, but we're going to make a list of good dogs, so that's what we're calling it.
Your computer will probably take a little while to generate the application, but when it's done, hop into your new folder and check out all the work that's already been done for you.
There's a lot going on here, and it can look really intimidating, so let's talk about what we want to focus on.
First let's look at the app folder. This is where you're going to be doing most of the work for these basic projects.
Rails is a MVC Framework. So most of what we'll be doing are in the models
, views
, and controllers
folders. One of the great things about Rails is that it's really smart as long as you use proper naming conventions. For instance if you have a file in models
called dog.rb
it will know to look for a dogs_controller.rb
in the controllers
folder. And the methods in your controller know to look in views/dogs
for matching files. So the index
method will look for a index.html.erb
to render.
I'll go into this a little more when we make our CRUD API.
There are two more folders we're going to look at very minimally for this tutorial. Let's glance at the db
folder first. In here we can check out our migrations, which is how Rails manages changes to our database. Or use seeds.rb
to create some initial entries in our database.
We'll also need to just pop into our config
folder and look at our routes.rb
. There's a lot of great stuff going on in here that pertains to setting up your environment and Rails has some great ways to separate your different environments.
But that goes beyond my scope here. The routes file is all we're really going to interact with here, and it handles all of the routing in your app, so we'll be using that for our CRUD API in a bit.
Now that you have a basic understanding of the file structure of a Rails application, let's start up that server.
Run rails server
which can be abbreviated as rails s
. Now navigate to localhost:3000
in your browser.
That's it.
Granted this is just Rails' basic example page. But I'll go into how to customize it in the next part of this series.
Posted on September 26, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.