This content originally appeared on DEV Community and was authored by Shaher Shamroukh
Rails Commands are very helpful and important to develop the application faster & easier and there are some great commands in Rails without whose commands we can’t even develop the application.
In this guide we are going to explore some of the best & must known Rails commands which will definitely help every Ruby on Rails developer out there.
So without further ado let's get started.
1- Creating a new application
rails new app-name
this command will create a new app.
rails new app-name -d postgresql
specify postgresql as the database of the app.
rails new app-name –api
creates only API application.
2- Running rails server
rails s
which is short for rails server
will run the server and open http://localhost:3000
3- Rails Generator commands
Now the following commands will help you to create assets, controllers,models, migrations, helpers, jobs, mailers, test files, scaffold, tasks.
so we are going to mention here the most important ones and to see the full list of commands run rails generate -h
Scaffold Generator
- rails g scaffold book name price:decimal
Model Generator
- rails g model ModelName column_name:datatype column_name2:datatype2
Controller Generator
- rails g controller ControllerName action1 action2
Migration Generator for adding columns
- rails g migration add_name_of_column_to_table_name name_of_column:datatype
Migration Generator for Removing columns
- rails g migration remove_column_name_from_table_name name_of_column:datatype
Remember to run rails db:migrate
after all of these commands
Here is a more in-depth discussion of what files each of these generators create, and when they are the most useful.
4- Opening Rails console
rails console
or rails c
The command let us interact with the application from the command line.
the rails console uses IRB.
This is very useful for testing out quick ideas with code and changing data server-side without touching the website.
5- Rails destroy command
rails destroy
or rails d
command is the opposite of generate.
It'll figure out what generate did, and undo it.
for example this command rails g model ModelName
will generate the model and to destroy this model all we do is rails d model ModelName
6- Rails about command
rails about
command will give us all the information we need about the current application as Ruby, RubyGems, Rails, the subcomponents, application's folder, the current environment name, Middleware, app's database adapter, and schema version.
7- Rails db commands
rails db
we use this command to create, reset, drop, run the migration and more, checkout the migrations guide Here
8- Displaying all Rails routes
rails routes
command will list all of your defined routes in a nicely organized way, and that is really useful for tracking down routing problems in the application, or giving you a good overview of the URLs in an app you're trying to get familiar with.
9- Running Rails test
As we know Rails comes with a test framework called minitest. The rails test
will run the different tests in the app.
10- bundle update
bundle update
command will update all of your Gems to match the Gemfile, If you modify the Gemfile in a project in order to include new or different Ruby Gems.
Resources
The Rails Command Line Guide
Also Here is a Cheat-sheet for Singular or Plural? in Ruby on Rails.
Summary
You’ve learned about the most useful command in rails so you can quickly jumpstart a new Rails application to practice with.
Also with practice those commands will be second to nature for you.
Thanks for reading!
This content originally appeared on DEV Community and was authored by Shaher Shamroukh
Shaher Shamroukh | Sciencx (2021-08-04T19:54:10+00:00) Top 10 Rails Commands We Must Know. Retrieved from https://www.scien.cx/2021/08/04/top-10-rails-commands-we-must-know/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.