Beginner’s Guide to Ruby on Rails Generators: Start your Rails Application in 3 Steps
Starting a new project is not the easiest task. Especially if you are new to Rails. Wouldn’t it be great if someone could just do some of the codings for us? Then Rails generators are what you need.
With a one-line command in your terminal, Ruby on Rails can create a model, controller, and view files associated with your application! You can even go one step further and add your controller actions and basically get a working app in a couple of minutes by using a Scaffold generator. So let’s start our Rails App by using different generators!
STEP 1 - Before starting using generators, think through your models, attributes, data types… Keep in mind the generated code adheres to RESTful conventions, as well as Model-View-Controller (MVC) patterns.
STEP 2- Start your rails app by simply typing:
rails new your-app-name
Then make sure that you are working in the right folder
cd your-app-name
Now you can check the list of generators simply by typing
rails generate
STEP 3- Go back to step 1 and decide which generators might best serve to create your M-V-C purposes.
- MODEL GENERATOR
The rails rails generate model NAME
command creates your migration and a corresponding table in your database and creates the attributes based on what you pass in after the NAME
of your model.
rails generate model Name column_name:datatype --no-test-framework
A couple of quick notes: Rails is smart enough to understand “g” stands for generate, so you can just type rails g model Name column_name:datatype --no-test-framework
If you are not planning to create tests for your app do not forget to add --no-test-framework
You can add multiple column names rails g model Name column_name:datatype column_name2:datatype2 column?name3:datatype3--no-testframework
DATA TYPES
some of the most common data types you can face are listed below:
:string
for smaller textual data like a username
:text
is for longer texts like comments, reviews and etc.
:integer
for whole numbers
:bigint
for arbitrarily large integers
:boolean
for true or false values
:datetime
for storing date and time
2. MIGRATION GENERATOR
What if you forgot to add a column to your table? Don’t worry, we have the migration generator to come to our help.
:rails g migration add_NAME_OF_COLUMN_to_table_name name_of_column:datatype --no-test-framework
3. CONTROLLER GENERATOR
More likely than not, we will need a sessions_controller in our projects so it is always handy to use controller generators
rails g controller Controller_Name
So it would look like this to create a sessions controller:
rails g controller sessions --no-test-framework#AND it would create everything you see below.create app/controllers/sessions_controller.rbinvoke erbcreate app/views/sessionsinvoke helpercreate app/helpers/sessions_helper.rbinvoke assetsinvoke scsscreate app/assets/stylesheets/sessions.scss
4. RESOURCE GENERATOR
This might be the ideal generator if you don't want to deal with the extra code that comes with scaffolding and is very handy in most cases. The resource generator creates a new Model, corresponding database table, controller, and an empty views folder.
rails g resource ModelName column_name:datatype --no-test-framework
This might be a common example:
rails g resource User username:string email:string password_digest:string --no-test-framework#which will create create db/migrate/20200919004751_create_users.rbcreate app/models/user.rbinvoke controllercreate app/controllers/users_controller.rbinvoke erbcreate app/views/usersinvoke helpercreate app/helpers/users_helper.rbinvoke assetsinvoke scsscreate /assets/stylesheets/users.scssinvoke resource_routeroute resources :users
5.SCAFFOLD GENERATOR
Basically, the Scaffold generator creates:
A- The full set of your model.
B- The database migration for that model,
C -The controller with full actions — even the actions you do not need.
D- Views — including the views that you are not going to use. (Needlessly to say, indexing your users is a terrible idea in most cases)
So let’s fırst look into the command line structure.
rails g scaffold ModelName column_name:datatype --no-test-framework
Let’s say if you are working on a Books app this will create
This example creates:
- A Books Controller
- A Book model
- A new resources:books route added to your config/routes.rb file
- A set of test files
- View files under app/views/books
Trivia — Did you know Since Rails 3.0, generators are built on top of Thor API?
6.DESTROYING GENERATED FILES
But what if we accidentally create a file or we use singular instead of plural?
We can just use the format below:
rails destroy generator_type accidentally_created_file
Rails is smart so if you want to destroy the files you created by scaffolding books
rails d scaffold books
Happy coding and please let me know if you have any comments or suggestions!
Sources:
https://edgeguides.rubyonrails.org/active_record_migrations.html