When first learning how to program, a lot of the projects you start with are already halfway finished. You might be learning from a Udemy course that gives you a default file structure, or maybe you’re enrolled with a Bootcamp that frequently starts you off with some pre-written code in an IDE. You’re comfortable working on a partially-competed project, but when you yourself have to create everything from scratch, you’re not always exactly sure how to light the fuse. This is a step-by-step guide for how to generally go about the process of creating a basic Rails app from scratch. For our demonstration, we’ll pretend that we’re building a basic Uber app where the relationships are as follows:

Image for post

1. Rails New Appname

Move into the directory where you’d like your project to be located and run rails new  into the terminal.

Image for post

This is going to give you a ton of structure that will allow you to get to the more customized parts of your project a little faster. In a nutshell, here are a few of the most important things that Rails is giving you “for free” when you enter this command. It’s good practice as a beginner to check these files out before doing anything else with your project.

All three of these files will be blank to begin with, but it’s good to locate them before you need to use them. As we move forward, you’ll be checking these files to ensure that your program is being updated correctly. For right now, just make sure these folders exist.


2. Generate resources for non-join models

For our example, since Ride is the join model between Driver and Passenger, we’ll begin by working on the latter two. This means we will do all of step two for both Driver and Passenger before moving on to step 3.

While you may prefer to add your models, controllers, and views folders with separate commands, rails generate resource allows you to combine these steps into a single command. The structure is as follows:

rails generate resource ClassName first_attribute:integer second_attribute third_attribute:boolean

Image for post

Notice that when declaring your attributes, any attribute that is a string may simply be declared without a colon. In this example, since the driver’s name will be a string, we do not need to include a colon followed by the data type.

Getting the Ball Rolling With a Basic Rails App
1.05 GEEK