At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

, ,

13 Rails Command Line We Must Know

  • By Venkata Krishna
  • October 26, 2018
  • 1527 Views

Commands are one of the best source to develop the application faster & easier and there are some best commands in Rails without whose support we can’t even develop the application. Today am planning to explain some best & must known Rails commands which will definitely help every Ruby on Rails developers out there.

1) rails new application-name

 

  • A well known command in rails used to create new rails application.
  • It will create all the directory structure, default configuration files, everything that you need to start the web application

Also there are many options we can pass inside the rails new command, for more info try rails new -help
Examples:

  • rails new -d postgresql – This will make postgres as your application database
  • rails new –skip-test – This will skip your test files creation
  • rails new –api – For only Api applications
  • rails new –skip-javascript – This will skip your JavaScript files

 

2) rails server

  • We all know that a web server is compulsory to run any web application so to set all this you can use this command & it will start your web server.
  • By default rails comes with one web server, this web server may vary depends upon the rails versions, Currently the latest rails version uses puma.
  • We can also use environment option -e to run the application in development, staging, production or testing but by default it will run in development mode.
  • we can also use -p option to run the application in different port number, by default it will run in 3000 port number.
  • You can also start your application by rails s, “s” is the alias of server.
  • For more information type rails -h or rails –help.

Examples:
rails s -e production -p 3001
rails s -e development -p 3002

3) rails generate

 

  • This command will help you to create assets, controllers,models, migrations, helpers, jobs, mailers, test files, scaffold, tasks.
  • You can also use rails g, “g” is an alias of generator
  • To check list of available generators in Rails you can use rails generate command
  • Using generators will save you a lot of development time, it automatically creates files, code for you.
  • If you don’t know how to use a controller/model/helper/mailer/ generator, type rails generate controller/model/helper/mailer in the terminal, it will guide you how to create controller/model/helper/mailer and lists all the available options.
  • You can pass different options like –skip-routes to the controller – this will not add routes to config/routes.rb file.
  • You can also pass controller action names in the generator, this will also creates routes for your actions.
  • rails g controller new edit index – new,edit,index are controller action names.

 

4) rails console

 

  • With the help this command you can interact with your rails application from command line
  • If you are not sure about your code standards at runtime then you can test your code in the rails console without any website interaction.
  • With this you can also interact with all the DB tables of your application through command line
  • You can also pass the -e option to the rails console to access the tables from different environments like development, staging, testing, production

 

  • For example, to start a console in production mode you can use rails console -e production
  • If you want to test some tables by adding some dummy data and you don’t want that data to be stored permanent in your table then you can run the rails console in sandbox mode.
  • rails console –sandbox, this will rollback all your changes on exit.

 

rails console

 

5) rails dbconsole

 

  • As you guys already know that we will use different databases for different environments so sometimes we might be confused, this command will help you to find out which database you are using.
  • We can also pass -p option to login to database of different environment.
  • For example, to start db console in production you can use rails dbconsole -p production.

 

Related: Active Jobs In Rails – Ruby On Rails Guide

 

6) rails runner

 

  • There will be some tasks which we no need to run at the time of normal request response cycle of your web application, in this case rails runner command will help you to complete your task.
  • Some best uses of this commands are, import/export of data, sending emails, scraping scripts.
  • rails runner Comment.all.map(&:title).each { |a| puts a } , this will print all the titles of your Comments table.

 

7) rails destroy

  • This is the vice versa of generate command.
  • By mistake you generated one model/controller, it adds many files like controller, views, tests, helpers, JavaScripts, Stylesheet and so on. So if you try to delete it manually then it will take some time and we may miss some of the files to delete, instead of all, if we use this rails destroy model/controller-name then it will destroy all the generated files.

 

8) about

 

  • bin/rails about(rails 5+ version), rake about(rails >5 version)
  • This command will help us to find all the versions we’re using in our application, it will show the rails version, ruby version, ruby gems version, rack version, and also it will show you the details of environment, database and schema version.

 

rails about

9) Assets

 

  • If you want to run the application in production mode definitely you need to pre compile the assets. For that you can use the following commands which will help you to deal with assets.
  • bin/rails assets:precompile (rails <5 version), rake assets:precompile (rails >5 version), this command will precompile your assets.
  • bin/rails assets:clean (rails <5 version), rake assets:clean (rails >5 version), this command will help you to remove existing compiled assets.
  • bin/rails assets:clobber (rails <5 version), rake assets:clobber (rails >5 version) If you want to clear the assets which are inside public/assets.

 

Related: Active Storage In Rails 5.2 With Real Time Example

 

10) Notes

 

  • bin/rails notes, this command will search for the comments starting with TODO, FIXME or OPTIMIZE in all your Rails application files.
  • It will check all the files whose extensions are with in .rb, .rake, .css, .js. .erb, .ruby, .yml, .yaml, .bundler
  • For example, just assume that you have some tasks which is to be done later or you have to revert some changes after few days, in that type of case if you add the comment starting with TODO, FIXME or OPTIMIZE, then it will keep track of all your TODO/FIXME/OPTIMIZE list.
  • If you still not clear then have a look at the below example,

Example: # TODO: Need to remove this after verification …

  • It will display file name with line number of the comment.
  • If you want to get only list of FIXME or OPTIMIZE or TODO we can pass like bin/rails notes:fixme, bin/rails notes:optimize or bin/rails notes:todo
  • You can use custom annotations instead of TODO, FIXME, OPTIMIZE by using the command bin/rails notes:custom ANNOTATION=Feature(feature is my custom annotation) and you can access all your custom annotations by using bin/rails notes:custom
  • By default rails notes command will look only in the app, config, db, lib and test directories, if you want to search in other directories we can configure using the config.annotations.register_directories(“vendor”)

 

rails notes

 

11) routes

 

  • rails routes(rails <5 versions), rake routes(rails >5 versions)
  • This command will display all your routes in your application, it display request type(GET, POST, PUT/PATCH, DELETE), URL, controller and action which you configured for route
  • If you want to get the routes of particular controller called ‘UsersController’ you can simply use rake routes | grep Users.

 

12) Tmp

 

  • tmp is the directory of your application is used for storing temporary files like cached data, processed id files.
  • rails tmp:cache:clear ,clears the cached data inside tmp/cache
  • rails tmp:sockets:clear ,clears the sockets inside tmp/sockets
  • rails tmp:screenshots:clear, clears the tmp/screenshots
  • rails tmp:clear, clears all the cache, sockets and screenshot files
  • rails tmp:create, creates the tmp directories for cache, sockets and Pids

 

13) Other Useful Commands

 

  • rails stats, this command will help you to find out the statistics of your application code
  • I will display number of classes, number of lines, number of methods your controllers, models, helpers, workers, mailers using.
  • rails time:zones:all, gives all the list of Rails known time zones
  • Also every rails command has –h or –help option which will guide you with several options.

rails stats
Liked it? Similarly find more informative blogs on our largest blog repository, Stay updated with latest topics & tricks and don’t forget to subscribe us to get the latest updates from diverse technologies. Besides all, post us your valuable thoughts in the comment section. For any queries reach us via info@agiratech.com.