Austin
Posted on February 27, 2023
Here are some of my most used Rails shortcuts and their syntax cheat sheet as a Ruby on Rails novice!
To start, if you return rails
on your command console, you’ll get a fairly extensive list of rails commands
Running rails [command] -h
(help) will provide additional details about the specific command.
This is helpful for any commands that are infrequently used. In your day to day, you’ll typically interact with a smaller subset of rails commands.
Getting Started:
Creating a new Rails application:
rails new [your-app-name]
This will create a new rails application with the given name [your-app-name]. The resulting artefact is a directory with a full rails app.
Starting your rails server:
`rails server` (or) `rails s`
This starts your rails server on localhost:3000 . Your local machine will typically automatically open a browser to the localhost:3000.
Rails Generator
Rails generator is a command that allows you to create different types of rails assets from your command terminal. Rails will automatically nest the resulting asset in the correct directory (i.e. controller file in the Controller folder) and set up the class.
rails generate [asset]
(or) rails g [asset]
Model
rails g model [ModelName] column_name:datatype column_name2:datatype2
This command shortcut creates a rails model.
**Remember that model names are SINGULAR.
This command also creates a corresponding migration file. The command also allows you to include the column names and column data types (if your column is a string, you do not need to specify a data type).
ex. rails g model Blog blogger_name age:integer blog_text:text
We would expect to see a Blog.rb model file and a create_blogs.rb migration file with the following data columns and data types:
blogger_name | string
age | integer
blog_text | text
### Generate Controller:
rails g controller [ControllerNames]
This command shortcut creates a rails controller.
**Remember that controller names are PLURAL
Generate Serializer
rails g serializer [SerializerName]
This command shortcut creates a rails serializer.
**Remember that serializer names are SINGULAR
Rails Database Commands:
If you’ve run ‘rails’ in the command terminal, you’ll have seen rails commands that start with ‘db:’ These are database commands that you are able run for your rails backend.
Here’s the full list you’ll see when you run ‘rails’:
db:create
db:drop
db:environment:set
db:fixtures:load
db:migrate
db:migrate:down
db:migrate:redo
db:migrate:status
db:migrate:up
db:prepare
db:reset
db:rollback
db:schema:cache:clear
db:schema:cache:dump
db:schema:dump
db:schema:load
db:seed
db:seed:replant
db:setup
db:structure:dump
db:structure:load
db:system:change
db:version
However, it's been my experience that some are more useful than others (much like the rest of the rails commands!):
Some common rails database commands:
rails db:create
Run this command FIRST and ONCE to create your database, allowing you to build your tables through migrationsrails db:migrate
This command executes the table creation and changes in your migration files.rails db:rollback
This command allows you to effectively undo your last migration, going back to the prior migration state (and the changes back the schema). This is particularly useful to undo mistakes you might have made in your most recent migration.rails db:seed
If you have a seed file with sample data to "seed" your database, this command will run that seed file. Note that that file must be independently set up and your DB should already be created and your tables set up with a migration.
Console:
rails console
(or) rails c
This command opens the rails database command console in IRB so you can interact directly with your ruby database.
This one is really nifty for quick sanity checks on your models and checking if your objects are correctly changing on the server-side without interacting through the application. This is particularly helpful if you haven't made necessary changes to the front-end to support the server-side change logic.
Routes:
rails routes
This command lists out all the RESTful routes as specified in your routes. The available routes will also be displayed in the command terminal in an organized format.
Prefix | Verb | URI Pattern | Controller#Action
It's especially useful as reference when you need the correct URI pattern/route for an API call (and the API call verb), as well as defining the action that you'll need to write in the corresponding controller file. It can also be helpful if you're getting ramped up on a new application.
Hopefully these shortcuts aid in your coding!
Posted on February 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.