Resource Generator in Rails

Photo by Joshua Aragon on Unsplash

In one of my previous blogs, I wrote a blog post on how to use the Model generator in Rails. In this blog post, I will explain how to use the Resource generator.

As we said before, generators in Rails is a built-in script that produces many application elements. The Model generator produces a model (class) inside the app/models directory, a migration file inside thedb/migrate directory, in addition to some unit testing files. However, the Resource generator produces more than just these elements. Let’s talk about this in detail.

You can write the command to create your resource specifying the attributes or you can leave them blank and specify that later in the migration. Let’s use an example to explain this:

$ rails generate resource Movie

This what happens when you run the previous command:

List of the files the command created

That created for us: a migration, a model, a controller, a route, a helper, and some test unit files. We will go over the migration, model, controller, and route.

The migration will be created for you, but it will be empty because you didn’t specify any attributes. Open the migration file, db/migrate/create_movies.rb
It should look something like this:

class CreateMovies < ActiveRecord::Migration[6.0]
def change
create_table :movies do |t|

t.timestamps
end
end
end

Here inside the change method, we can add each of the table attributes and specify what data types they will be. Let’s just add two attributes, the title and the director:

class CreateMovies < ActiveRecord::Migration[6.0]
def change
create_table :movies do |t|
t.string :title
t.string :director
t.timestamps
end
end
end

If you want to specify these attributes beforehand, you just need to add them to the resource generating like this:

$ rails generate resource Movie title:string director:string

The model will not have anything in it. It will an empty class that inherits from the ApplicationRecord class which inherits the ActiveRecord::Base. Later, in your project, you might customize this class by adding some methods to it.

class Movie < ApplicationRecord    # Methodsend

Like the model, the controller will also be an empty class that inherits from theApplicationControllerclass which inherits from the ActionController::Base. Later, you can add some actions such as index or showto this controller.

class MoviesController < ApplicationController    # Actionsend

The routes file, config/routes.rb, is basically a way to handle the HTTP requests and match them up with the controller actions. The resource generator adds all the basic routes inside the routes.rb.

Rails.application.routes.draw do
resources :movies
# For details on the DSL available within this file, see
https://guides.rubyonrails.org/routing.html
end

This was how to use the Resource Generator in Rails. If you have anything you would like to add, please comment below.

Full stack software engineer. Passionate about making people’s lives better and easier through programming. LinkedIn.com/in/shaqqour