The Definitive Command Cheat Sheet for Rails Beginners
When I was a newbie in Rails, the first couple of weeks I survived reading all the articles and understanding only like 30% of what I was doing. It was a lot of information, and one of the things that annoyed me the most was that when, after a lot of effort, I finally realized what I have to do, I have to look through all the readings and find the correct command that I need to write on my terminal (after all this time, now I can type them almost with closed eyes but at first it was incredibly tough).
So for this article, I will write what I would love to have when I was starting: all the most popular commands I used while I was learning Rails in a single place. I know they are not all of them, but I hope it would be most of the commands you will use at the beginning.
When creating a new rails app:
$rails new name
pretty straightforward, you create a new rails application and you give it a name.
Install the gem’s version specified in the Gemfile.lock, it will complain if you have incompatible versions.
$bundle install — without production
Exclude gems that are part of the production group.
update all your gem dependencies to their latest versions.
Environments and web browser:
Use if you want to access your application through a web browser. If it is local the direction usually is: http://localhost:3000
: lets you interact with your Rails application from the command line in the development environment.
$rails console — sandbox
Use it when you wish to test out some code without changing any data, any change will disappear when you quit.
$rails console test
: Run the console in test environment.
: Use it if you changed some source code and want those changes reflected in the console without having to restart.
$rails server — environment production
: Run a rails app in production mode.
$rails generate scaffold Post name: string title: string content: text
A scaffold is a set of model, database, controller, views, and a test suite for each one of them. Usually, you should include the name of the Model (in singular and first letter capital), and the parameters of the model. In this example, we create a model called Post, with the parameters name, title, and content.
$rails generate controller Posts or rails g controller Posts:
creates a controller, the name should be: first letter capital, and in the plural.
$rails generate controller Posts show:
if you do this, you will have the same controller as above, plus inside an action called show.
$rails generate model Post:
create a model, the name should be: first letter capital, and in a singular.
$ rails generate model Post name: string title: string content: text:
the same but also includes the attributes: name, title, and content.
: runs the migration of a model and it’s attributes.
$ rails generate migration migration_description
the easiest way to make a change in the database schema in Rails is to generate the migration. NEVER do a change directly on your database.
$ rails db:migrate:reset
This will drop the database information and runs migration on a fresh one.
Loads the data from the file: db/seeds.rb into the database. It’s a very useful way of populating a database with the initial data needed for a Rails project.
When messing things up:
$rails destroy model Post
Almost anything created with the generate command can be destroyed with the destroy command. In this example, I’m destroying a model called Post.
This undoes the last migration, you can then edit the file, and run rails db:migrate again.
$rails db:migrate VERSION=0
Use this to roll back all migrations back to (and including) a target migration. In this case, we use the version number 0.
$rails test or rails t
: run our test suite to verify if our test passes or not.
$rails generate integration_test site_layout
: Integration tests are used to test how various parts of your application interact. Here we are creating an integration test named “site_layout.test.rb” inside the “test/integration” folder.
$rails test: integration
only runs a specific section of tests, in this case, it will only run the integration tests.
To get a complete list of the available routes in your application.
Bonus: Deploying with Heroku
This is a simple step-by-step command line you should follow if you are deploying to Heroku and using Github. You need to remember that your app should be in a master branch on your Github repository for the command git push heroku to work. If not, you need to use the command:
$git push heroku yourbranch:master
- $ git status
- $ git add -A
- $ git commit -m “Description of commit”
- $ git push
- $ rails test
- $ git push heroku
- $ heroku pg:reset DATABASE
- $ heroku run rails db:migrate
- $ heroku run rails db:seed
- $ heroku open
Hope you enjoy it! Happy coding
Subscribe to get your daily round-up of top tech stories!