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.
: pretty straightforward, you create a new rails application and you give it a name.
$rails new name
: Install the gem’s version specified in the Gemfile.lock, it will complain if you have incompatible versions.
: Exclude gems that are part of the production group.
$bundle install — without production
: update all your gem dependencies to their latest versions.
: 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.
: Use it when you wish to test out some code without changing any data, any change will disappear when you quit.
$rails console — sandbox
: Run the console in test environment.
$rails console test
: Use it if you changed some source code and want those changes reflected in the console without having to restart.
: Run a rails app in production mode.
$rails server — environment production
$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.
: 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 generate migration migration_description
: This will drop the database information and runs migration on a fresh one.
$ rails db:migrate:reset
: 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.
: Almost anything created with the generate command can be destroyed with the destroy command. In this example, I’m destroying a model called Post.
$rails destroy model Post
: This undoes the last migration, you can then edit the file, and run rails db:migrate again.
: Use this to roll back all migrations back to (and including) a target migration. In this case, we use the version number 0.
$rails db:migrate VERSION=0
: run our test suite to verify if our test passes or not.
$rails test or rails t
: 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 generate integration_test site_layout
: only runs a specific section of tests, in this case, it will only run the integration tests.
$rails test: integration
: To get a complete list of the available routes in your application.
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
Hope you enjoy it! Happy coding