
Using the resource generator creates everything from controller to migration. Running rails g resource User username:string email:string age:integer will result in the following:
# a user controller
class UsersController < ApplicationController
end
# a migration
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.integer :age
t.timestamps
end
end
end
# a user model
class User < ApplicationRecord
end
# endpoints for the user resource
Rails.application.routes.draw do
resources :users
end
Sometimes using the resource generator is somewhat overkill. That is where individual generators come in handy. There are 3 that I have found to be very useful. The controller, migration, and model generators.
Using the controller generator creates... You guessed it! A controller. Running rails g controller Users will result in the following:
# a user controller
class UsersController < ApplicationController
end
If you wish to also generate actions along with the controller you can do so by including the name of the action in the command. Running rails g controller Users index show create update will produce:
# a user controller
class UsersController < ApplicationController
def index
end
def show
end
def create
end
def update
end
end
# routes each actions
Rails.application.routes.draw do
get 'users/index'
get 'users/show'
get 'users/create'
get 'users/update'
end
It has to be said that, at the time of writing this, I am unaware of a way to generate the correct HTTP verbs to go along with the routes. This is no big deal, you just have to remember to change the verb prefixing the route with whichever it needs to be, or by using the resource prefix like you get from running the rails g resource generator.
Using the model generator creates both a migration and model. Running rails g model User will result in the following:
# a migration
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.timestamps
end
end
end
# a user model
class User < ApplicationRecord
end
This is pretty bear bones, so we can spice it up by including an optional list of attribute pairs as arguments. Running rails g model User username:string email:string age:integer will give us the following:
# a migration
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.integer :age
t.timestamps
end
end
end
# a user model
class User < ApplicationRecord
end
Notice how the attribute pairs are broken up into two parts. This is so rails knows the data type of each attribute, but a small time saver is to leave off the datatype if the attribute is a string so rails g model User username:string email:string age:integer would become rails g model User username email age:integer. You can do this because the default datatype will always be a string.
Take note on the pluralization of the resource name when using each generator. For both the model and resource generators DO NOT pluralize the resource name, but go ahead while using the controller generator.
If you accidentally mess up while using one of the generators, it's an easy fix. Let's say I ran rails g resource Users, forgetting that "Users" needs to be singular, I can simply hit the up arrow on my keyboard and change g -> d resulting in the following command: rails d resource Users. This will destroy all files generated from the resource giving you a fresh plate to fix the typo and re-run the generator.
Lastly, knowing when to use each generator is important and can save a ton of time when developing with ruby on rails.
