This is a quick tutorial on how to start a new rails project and actually start seeing something in your browser. To start a new Rails application, all you need to do is type in your terminal, “rails new” and the name of your project.

Upon doing this, Rails will provide you with the entire environment that you need in order to get started! First thing I like to do is look at what Rails gave me, and understand “the code” that was written for me. Let’s take a look.

Here we have the “app” folder at the top, this were we will be doing most of the work. Within the “app” we will focus on “controllers”, “models”, and “views”. In Rails we follow what’s called the “MVC” pattern.

MVC stands for Models, Views, Controllers. We control the flow of our code through our Models, Views, and Controllers. We start from the browser, it makes a request, that request gets sent through our routes, into our controller, then into our model which in turn gets the necessary data from the database. Then we go back from our model to our controller, into our views, render there, back through the controller back into the browser.

Rails Generators

To see all of the available options that Rails generators has simply type into your terminal “rails g”, this will show you all of the possibilities you have.

If you start a brand new project, the quickest way to get yourself actually coding is “rails g resource model-name”. This will give you a place to write your code, in the models, views, and controllers, as well as your tables for your database. But if you do not need all of those, the use either “rails g model ‘model-name’”, “rails g controller ‘model-name’”, or “rails g migration”. Using rails g model, this will give you your model and table for your database. Rails g controller will just give you your controller. Rails g migration is used to change your database, or table within your database. There are more options, these are just some examples.

Rails Routes

In order for your Rails application to function correctly, you must specify the routes it must take, this is done in your “routes.rb” file under config.

Routes can be created in either one of two ways, there is the custom route, where you write out all the routes, or the quicker way and that is by using the “resources” key-word.

Now using the key-word “resources’ is a lot simpler and quicker yes, but this can only give you the 7 basic “RESTful” routes which are “Index, Show, New, Create, Edit, Update, and Delete”. If you want custom routes to show your user something else such as for login or logout, you will have to write out custom routes. Therefore, I believe it is very beneficial to know how to write out custom routes.

After we have finished our routes, we move on to our controllers and views. First, let’s go through our Controller. Here we create the methods the line up with our views, not all methods have a view, but that is because they are connected to another method and it will redirect to a different method. For example, you should not add a view for create, because you will handle the creation of an instance and then probably redirect to its show page.

Take a look at the following code, here I have layed out the basic methods you should have in order to have a full “CRUD” app in a controller.

Let’s go through each method, (order does not matter, this is just the order I put it in) our index method is just finding all the instances of one of our models, that we want to show to our users. Next we have “new”, here we are setting the instance equal to a new version of the model, and then we send it to the user to add in the information they want to add, then it gets sent to our create method. In the create method we are using strong params, they are the params that are nested one level when the user submits them, we check if the information they sent us is valid based on validations we set to protect our database. Followed by the show method, which just show a single instance, we can use a before action for this method, because we want to use the same code in two other methods, (i.e. our edit and delete methods)The code is as follows:

We would put this method under the “private” key-word, meaning this method can only be called from within, on the same page you wrote the method.

This is just scratching the surface of the power you have with Rails. But to continue learning how to build a rails app, I would suggest to learn about validations from:

Resources:

https://guides.rubyonrails.org/