Getting a Rails Back End Up and Running
Rails puts you on the fast track in app development
I’m relatively new to coding so I’ve had the benefit of working with advanced technologies that have abstracted away a lot of the complex time consuming tasks of building apps. Rails is one of these advanced technologies. Before Rails, programmers spent, hours, days, and even weeks building their environments and setting up dependencies. Using Rails enables programmers to do all of that with simple single line commands in the command line, completing all of those tasks in minutes or even seconds. This frees the programmer to focus on actually designing and building the functionality of the app.
I must admit that when I first encountered Rails, it appeared very intimidating. I didn’t realize or truly appreciate all the heavy lifting it was doing behind the scenes. But now I appreciate it being available when I came on the coding scene because it has saved me time and frustration in building my apps.
So what is Rails? The documentation states:
Rails is a web application development framework written in the Ruby programming language.
Simply put, Rails is a framework used to develop or build web applications and it was created using the Ruby programming language. Since Rails is dependent on Ruby, it does require that you have Ruby installed on your computer. It also requires you have SQLite3, Node.js, and Yarn. And of course, you have to actually install Rails. You can consult Ruby Guides for all of these installations.
After you successfully install Rails, go to your command line and navigate to the location you want your Rails back end to reside. Then enter:
rails new app-name --api
That’s it! This single command will build out the file structure for your Rails back end application. The built in generator “rails new” is a script used with the name you give your app to build a new Rails application. The api flag (- -api) is used to configure your rails back end to be used with a front end.
NOTE To build a CLI application that doesn’t require a front end, you can omit the api flag and simply use:
rails new app_name
So if I wanted to create a Rails application that tracked a list of all the things I wanted to accomplish in my lifetime, I could use the following:
rails new bucket_list --api
This generates a Rails back end called Bucket List. To build out the functionality that allows me to add items to my Bucket List, I create a table to store my items on the database. To do this, I first have to move into my newly created Bucket List back end using the following:
cd bucket_list
Then I use another generator called scaffold:
rails generate scaffold List item:string
This one simple line creates a migration, a model, a controller, and routes. The migration contains building instructions for a table named List containing a single item column. The model contains a List class that will create List instances. The controller contains actions which are methods responsible for carrying out CRUD (create, read, update, delete) actions for the List. All of the List routes (for receiving requests from the front end and routing them to the proper controller and action) are stored in the single line resources :lists.
The following commands will create a database and then run the migration to actually build or create the List table:
rails db:create
rails db:migrate
Rails auto generates a schema of this newly created table. To test that everything has been properly set up, I create some mock or seed data in my seed file using the following:
List.create(item: “Get a Rails back end up and running!”)
To actually add this first item to my Bucket List, I run the following in the command line:
rails db:seed
Now I start my server with the following:
rails server
This will be running on localhost port 3000. So to see the results in my browser, I enter the following URL address in my browser address bar:
localhost:3000/lists
Hit enter and I see a List object containing my single Bucket List item to “Get a Rails back end up and running” is stored in an array. My first Bucket List item is now complete, my Rails back end is up and running!
Now that I’ve successfully created a Rails back end and it’s up and running, all I need to do is build out the front end and get the two to communicate and I’ll have a fully functioning Bucket List application.