Let’s Set Up a Basic Rails App!

Meredith McIntosh
9 min readDec 21, 2020

Listen — coding is tough. Learning to code is tough. Manipulating things that have learned in a video or article is tough, but have no fear! With consistent practice, it does get easier. Hmmm, maybe easier isn’t the right word. Let’s say it gets more manageable! More exciting! More fun!!!

Today, I am going to do a tutorial on how to set up a basic rails app! Let’s get started! This is gonna be laid out step-by-step so you know exactly what to do to be successful! But remember, this is simple! This is for someone just starting out and has some basic knowledge but needs a little guidance!

*All throughout the article, there are links to explain certain parts of the process further if needed.*

Wait!! Before we even begin, let’s decide what our many-to-many relationships are going to look like! Hmmmm…. I love pugs and I love parties… OH! I am going to do Pug Party app!! I am going to have three models — person, pug, and party! But what are my relationships going to be?!

Okay, I think the following would be perfect for a simple app! -

A person has many parties and a person has many pugs through the party.

A pug has many parties and a pug has many people through a party.

The party belongs to the pug and the person.

Then, I am going to go ahead and think through some attributes that each object should have. This is very helpful when it is time to set up migrations, models, controllers, seeds, and honestly, the rest of the app.

Attributes —

Pug — name, age

Person — name, age

Party — date, location, pug_id (pug that is associated with this party), person_id (person that is associated with this party)

If the above seems like gibberish to you, I recommend clicking the “many-to-many relationships” link above to the documentation that can explain further.

  1. Open up Visual Studio Code (VS Code) or whichever code editor you prefer!
  • If you don’t have a code editor installed on your computer, click this link to begin the process of installing VS Code!

2. Open up the terminal (short cut → CTRL ~)

3. In the terminal, make sure you are in the directory you want to file your new project and then type → rails new name_of_project

This command is going to download A TON of rails files that are going to be beneficial to your in creating the app.

4. Once you have done that, you need to cd (change directory) into your new file! I named my file pug_parties, so I will type the following into the terminal → cd pug_parties. This will now show that I am in the correct directory. Now go ahead and type code . into the terminal. This will open up the new project file in VS code!

5. Wow! Now we have a TON of files available to us on in the left side bar! WOAH! At first, all of those files seem very overwhelming and stressful, but trust me — with practice, you will be flying back and forth between files without even really thinking about it!

6. Now that we have our project ready and available to us, let’s begin setting up our migrations.

Our migrations will be stored in the db (database) folder. We can actually create our migrations very simply in the terminal. I went ahead and added on my attributes at the end. Name is a string and the terminal doesn’t need me to tell it that (it will just be assumed), but the age is an integer so I included :integer next to age.

When I click enter, this is going to automatically set up a migration for my pug model!

This will need to be done for all three models! *Remember a pug_id and a person_id is an integer! Once all migrations are set up, run rails db:migrate in the terminal. If you get something like this below, you are doing great!

Before we go on to the next step, take a peek at the newly created schema. The schema is located in the db folder. I am still extremely new to rails, but one thing that I know for certain is — NEVER EVER EVER TOUCH THE SCHEMA. If you need to adjust anything, do not do it in the schema!!

7. Once all your migrations are set up, let’s create our models! The models are in the app folder. I think these are very easy to do manually. The model files should be singular, so pug.rb, person.rb, party.rb. Below is what they should like! The models are extremely important. This is where our associations come together!

8. Now, let’s go ahead and set up our controllers. We can set up the basic controller by typing the following in the terminal — rails g controller friends. Be aware that the controller names are plural! We can do this for all three models. Now, we have all three controllers set up and ready to take in actions/methods.

9. Now, it is time to start creating seeds! Seeds are the data that you are going to be working with in your app!

First, I ALWAYS put .destroy_all at the top of the seeds file. Because if for any reason we get doubles or triples of the same data, the .destroy_all method will remove any duplicates!

Then, plug in all the data! Below is my seeds data. Then, run rails db:seed to plug all the data into the database. If you return no errors, the seed should be correct!

10. Now, let’s check associations to MAKE SURE that everything is running properly. We can do this in the console. So in the terminal type rails c OR rails console.

Once you do that, test to see if all our data is in the database and associated correctly by typing Pug.all (this should return all pugs), Person.all (this should return all the people), Party.all (this should return all the parties), and finally, try Person.first.parties (this should return the first person and the party that they are associated with).

After testing my seeds, everything looks correct! We are on our way to having a super simple rails app functioning properly!

11. It’s time to make sure we are connected to rails. Make sure you have exited out of the console by typing exit. Then, type the rails s in the console. If you get something similar to below, the server is running properly and is ready to be viewed in the browser.

Go ahead and open a new browser, and type in localhost:3000. If this is what pops up in the browser — YAY! Things are running smoothly.

But this is not what we really want or need. This is a good sign that everything is working smoothly, but now it is time to actually show ALL THE WORK we did in our code. We need to finish our controllers and our views!

11. The controller is where we put the actions/methods and then we can put in html and embedded ruby in the views to make it viewable and usable by the user. So, let’s go back to the controllers and put index and show pages in all the controllers. The following is what they should look like! This is just an example of the pugs controller. Now let’s go ahead and looks at the views!

12. You should find the views folder inside the app folder. The views is what is actually visible to the user (in the browser). Because this is just a simple rails app, let’s just set up an index and show page for each model. Index for a model will show all the data. Then, the show page, will be specific to one of the objects in a model. Below, is all the folders and files set up and ready!

Before, we can view anything in the browser, we still need to set up our routes! SO IMPORTANT to have all the pieces set up properly. The routes folder can be found in the config folder. Right now, for the sake of simplicity, I am just giving access to all the basic routes by doing the following —

Routes are vital to be able to show anything in the browser.

Now, that we have our routes set up, let’s put all the basics in our views folder. For this article, I am going to show the pugs index and show page. Below is what the index file should look like! The index page will list out all the pugs in our database. I also used a link_to method to be able to click the pug’s name and go to the pug’s individual show page.

Then, the show page below will give all the info we have available about that specific pug. The pug is simple. We have two attributes. We have the pug’s age and name. We definitely can add WAY more info. For example, we can put the people the pug parties with on this page and we also can put the parties the pug will be attending on this page. However, because we are being super super super simple, we just want a basic rails app functioning well!

To be able to see the pugs index page, go to your browers and add /pugs.

As you can see, the pug names are links. When I click on Frank, this is what I see! Do you see the /1? That is showing what Frank’s id is! Wahoo!

This is a super simple rails app, but it should get you started and on your way to adding more details and more fun user capabilities! For example, you can add images, forms, validations and honestly so much more! But for now, I hope you enjoyed this simple rails app!

*For further guidance and documentation, please visit the rails documentation site or if you need more clarification on MVC, please click the link.

Happy coding!

Let’s connect on LinkedIn!