Beginner’s Guide on Ruby on Rails Generators: How to Get Your App Up & Running
Starting a new project is not the easiest task. Especially if you are new to Rails, and running between projects. Well, wouldn’t it be great if someone could just do some coding for us? No, we don’t need caffeine driven Jim Carrey with some divine powers! All we need are Rails Generators!
With one 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. However, if you are a beginner, Scaffolding comes with a warning. 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 if the ultimate goal is to learn, have an effective, DRY code that other programmers (and yourself) could easily understand, scaffolding might not always be the best idea. We will return to scaffold later on but let’s first start our Rails App!
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 simply typing
rails new your-app-name
Then make sure that you are working in the right folder
Now you can check the list of generators simply by typing
STEP 3- Go back to step 1 and decide which generators might best serve to create your M-V-C purposes.
- MODEL GENERATOR
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
NAMEof 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
You can add multiple column names
rails g model Name column_name:datatype column_name2:datatype2 column?name3:datatype3--no-testframework
some of the most common data types you can face are listed below:
:stringfor smaller textual data like a username
:text is for longer texts like comments, reviews and etc.
:integerfor whole numbers
:bigint for arbitrarily large integers
:boolean for true or false values
:datetimefor 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
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 -create app/controllers/sessions_controller.rbinvoke erbcreate app/views/sessionsinvoke helpercreate app/helpers/sessions_helper.rbinvoke assetsinvoke scsscreate app/assets/stylesheets/sessions.scss
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. 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-frameworkwhich 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
Now let’s revisit our friend Scaffold. To generate a
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?Thor provides powerful options for parsing and a great API for manipulating files.
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
Looking into generators really helped me with my project and I hope you are now feeling more confident to use Rails generators. Happy coding and please let me know if you have any comments or suggestions!