New to Rails Generate Controller & Model?

If you’ve gone or are going through a bootcamp (like I did), you were exposed to a lot in a short amount of time. I don’t know about you, but I sometimes felt that I was memorizing commands and patterns to keep up, instead of getting the time to build foundational understanding. Now that I have time to dig deeper, one thing I wanted to fundamentally understand instead of memorize was rails g controller and rails g model (g is a shorthand for generate in Ruby ).

This blog post will cover my current general understanding and hope that it will allow you to understand a little more as well. As always, our learning and understanding will grow, so I encourage you to share a response of what you currently understand about rails generators when you’re done reading.

After you create a new rails app using rails new whatever_your_app_name_is (ex: rails new cooking_blog), you’ll notice lots of new folders were created:

This image represents some of the files that were created after using the command rails new generators to start my ruby app. (Note: I used the name generators as my app’s name. This is no relation to the generators that will be written about, it’s just a name…I could have written rails new chicken.)

Notice I have certain folders open under the app folder: controllers, models, views/layouts, and db. This is where we will see changes when using rails g controller and rails g model.

Let’s start off with rails g controller Users as our example.

Rails Generate Controller

At this point I have used the command rails g controller Users. Make sure the model you’re making a controller for is plural while generating. Ruby has naming conventions for a reason. I can’t share why plurality matters in this case, but my logic is that the controller will have methods that will apply to all instances of the model generated (users in this case), so the command will show as such.

Take notice of the new files that have been added to some of the folders. Stop reading and scroll back and forth between the images for a visual understanding. Now we have controllers/users_controller.rb and views/users.

The controller is like a hub of that will hold actions. These actions can be viewing a specific user, creating a user, or updating a user. The actions exist through the methods of def index, def create, def update, etc. Another way to think about it is: The controller is where CRUD functionality happens, so the methods that describe certain actions live in the controller.

The view/users folder is where templates are added. I won’t go into this, but a quick explanation is view/users is where html.erb files are placed. Those files allow you to show content such a welcoming paragraph or a form.

The images to the left is what you’ll see when you open the users_controller.rb file.

The users_controller.rb file is where all of the CRUD action happens. This is where you add your methods such as def index, def show, def new, and other methods needed for CRUD functionality, as seen in the images to the left.

That was a basic explanation for rails g controller . It sets up a bare skeleton for a controller, which you add flesh to. There is more work needed for the controller such as setting up routes, but I don’t want to steer away from sharing the basic job of rails g controller and rails g model. With that said, let’s move onto generating a model.

Rails Generate Model

I will point out Ruby naming conventions again and with my reasoning behind it. The command rails g model User is singular. The way I think of it as if I’m going to create a blueprint (or model) of what a user is. I can follow this single blueprint to create many users. So, let’s create our blueprint now!

In this example, I would like a user to have the following attributes: name, age, and bio. We can generate a model and its attributes in one go.

rails g model User name:string age:integer bio:text

string, integer, and text are Ruby datatypes. You decide what datatype you would like your attribute to be. After you run the command in your terminal you should see some new files and folders.

The main new parts to notice are the new user.rb file in app/models/user.rb, the folder called migrate, and its file named 20200815012729_create_users.rb. The path is db/migrate/20200815012729_create_users.rb. The numbers represent a time stamp when the model was created, so that number will be different for you.

The image to the left is what the app/models/user.rb will look like. This is where you can add your associations, validations, etc.

This is what db/migrate/20200815012729_create_users.rb. Do you recognize something that seems familiar? Notice how rails g model User name:string age:integer bio:text created a nice users table which will help add data to the database and is related to the User model.

At this point, you have seen some of the basic additions that rails g controller and rails g model has to offer. Rails generators can be confusing at first, but with practice under your finger, you’ll start to love them. A tip is go follow the example in the Ruby Docs. If you’ve heard that before, tried, and still felt overwhelmed, that’s perfectly fine. I felt exactly the same, so you’re not alone. I decided to follow some guided tutorials on youtube to boost my confidence(not even Ruby related). After that, I was curious about Ruby Docs again, looked at it, and found that it was more approachable. I went through the example a couple of times until I said “yeah, I get that” more than “wait…what?”.

Keep practicing. If you feel like you’re not making progress, ask how you can learn another way and try that. You’ll find what works best through practice.

I hope this was able to clear some basics up about rails generators and encourages you to move onto more like rails g scaffold or even creating your own generator!

I’ll leave you some key points of the article.

Major takeaways:

  • rails g controller is where plurality matters. We get a controller for users and a view folder for users. The controller is where methods belong, such as def index, def show, def new, and other methods needed for CRUD functionality.
  • rails g model is where singularity matters. This is where you’ll get your model’s ruby file(i.e user.rb), in the models folder, to add your associations. You’ll get a folder to hold your model’s views under the views folder (i.e. views/users). You’ll also get a migration file (i.e. 20200815012729_create_users.rb).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store