Creating a Rails as API project pt 1: Set Up
Tags: rails, ruby, api, tutorial
Published: 2022-06-12 21:42:05



Over the next few blogs we will be building a Rails project. We will use Rails as an API. Eventually we will use it as the end point for a React project.

For this you will need Ruby, RubyGems, and Bundler installed on your system. I will be using [Visual Studio](https://code.visualstudio.com/) as my IDE.

[Ruby Docs](https://www.ruby-lang.org/en/documentation/installation/)

[RubyGems Docs](https://rubygems.org/pages/download)
```
$gem update --system
```

[Bundler Docs](https://bundler.io/v1.12/)
```
$gem install bundler
```


---
## Installing Rails

To install the latest version of Rails, run in the terminal:

```
$ gem install rails
```

In the terminal create a new folder for the project and cd into that folder, once inside we can create our Rails project. I will name mine rails-api-project.

```
$ rails new rails-api-project --api --skip-javascript
```
Once you run that command a lot will load:

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdgmlxb885qvw3miiial.png)
 
A lot just happened right there, the main points are:
- Rails generated a new directory with all the code required to build our application
- `bundle install` ran to download all the required gems
- `git` was initialized in the newly created directory


I used the --skip-javascript flag to tell Rails that we won't be using JavaScript for this project. We will use React for all of our JavaScript needs. Using this flag makes the installation faster.

---
## Running the Rails Server

To start the Rails server, make sure that you are in the root of the application in the terminal and run this in the terminal:

```
$ rails s
```
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/779zz3bb2ejyp4fzmb62.png)

Lets verify that it's working in the browser by navigating to [localhost 3000](http://localhost:3000) 

![Rails url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjfpqvro34vwev0x8tf5.png) 
![Rails page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfjhj7al5c3cr1nmys0k.png)
 
If you click on the image it will take you to the [Ruby On Rails website](https://rubyonrails.org/)

---

Fantastic! We have a Rails application running. It isn't doing much right now so lets get something of our own happening.

---

##Creating Routes

Here is what we should see in our IDE. These are all the files that Rails set up for us:

![IDE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqnktclnfwoc5ezj2azb.png)
 
Now lets click into the folder `config` then the file `routes.rb`. We should see:

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/errxid5xjs7a0i6uxxtc.png)

Now we need to define a route in our Rails app. A route will tell Rails that when a user makes a request with _this_ HTTP verb and _this_ path, run the code in _this_ controller.

Lets set up a route to cats

```
# config/routes.rb

Rails.application.routes.draw do
  get "/cats", to: "cats#index"
end
```

Here we are telling Rails that when a GET request to the /cats path comes in, run the index method in the CatsController.


---
##Setting up a controller

Let's create a controller now. Click on the app folder, then the controllers folder, then create a new file called cats_controller.rb and put the following code inside:

```
class CatsController < ApplicationController

  def index
  end

end
```

Since we are creating an API lets render some JSON.
Lets just write something for now so we know our code is working. 


```
class CatsController < ApplicationController

    def index
        render json: { love: "Cutie Cats~!!! <3" }
    end
  
end
```

Lets give it a test and check out [http://localhost:3000/cats](http://localhost:3000/cats)


![cats url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukvhb8vk914zwn9bbfex.png) 
![cats json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n863id8xibvs8yhpqehd.png)



If your JSON doesn't look pretty in the browser I recommend the Chrome extension [json-formatter](https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa?hl=en).

---
## Next Time

This was the initial set up of our Rails API.
Next blog we will get into adding Active Record to our Rails application so we can start adding tables and data.