December 18, 2018
Understanding the Ruby on Rails CLI
Ruby on Rails is such a vast framework. The Ruby on Rails CLI makes working with the framework that much easier. This video/article is the result of my own trials and tribulations while learning the framework.
Understanding the Rails CLI is beyond helpful when first creating a new application. Once you have Rails itself installed, you can open up a new terminal window and type
rails. There, a large list of options will appear to help guide you with setting up your app for the first time. The available options are virtually endless which makes configuring your app a walk in the park.
In my video example I created a new ruby on rails application by running:
$ rails new demo_api --skip-coffee -T --webpack=vue --api
This scaffolds a new app whilst doing the following changes:
--skip-coffee- skips using
CoffeeScriptand opts for regular
-T- skips adding any new tests when a model or resource is generated.
--webpack=vue- This tells rails to utilize webpack (which is now built-in if you want it) and also installs/configures the
--api- Configures the new application to be API only so every controller renders
jsonand there are no views besides mailers.
While this list is a great start, there are even more options you could pass to enhance or "enhance" your app depending on what it is you are looking to build.
Within a new application
Having created your new app you can again run
rails alone within your root app directory. This presents another long list of commands available. My demo app created in the video has the following. There's a lot of creating commands here that make our lives as ruby on rails developers that much easier. I invite you to get to know them more so your workflow can be faster.
The most common rails commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") server Start the Rails server (short-cut alias: "s") test Run tests except system tests (short-cut alias: "t") test:system Run system tests dbconsole Start a console for the database specified in config/database.yml (short-cut alias: "db") new Create a new Rails application. "rails new my_app" creates a new application called MyApp in "./my_app" All commands can be run with -h (or --help) for more information. In addition to those commands, there are: Rails: console credentials:edit credentials:show dbconsole destroy encrypted:edit encrypted:show generate new runner secrets:edit secrets:setup secrets:show server test version Rake: about active_storage:install app:template app:update db:create db:drop db:environment:set db:fixtures:load db:migrate db:migrate:status db:rollback db:schema:cache:clear db:schema:cache:dump db:schema:dump db:schema:load db:seed db:setup db:structure:dump db:structure:load db:version dev:cache initializers log:clear middleware notes notes:custom restart routes secret stats time:zones[country_or_offset] tmp:clear tmp:create webpacker webpacker:binstubs webpacker:check_binstubs webpacker:check_node webpacker:check_yarn webpacker:clobber webpacker:compile webpacker:info webpacker:install webpacker:install:angular webpacker:install:coffee webpacker:install:elm webpacker:install:erb webpacker:install:react webpacker:install:stimulus webpacker:install:typescript webpacker:install:vue webpacker:verify_install webpacker:yarn_install yarn:install
CLI built into Ruby on Rails was all too daunting to me when I first started. Over time, and after building a few apps, I have grown to love what it offers. Quickly configuring things, generating things, deleting things, and more are all too possible using the command-line tool. If you are a newcomer to Ruby on Rails you should fiddle with the CLI a bit on a test project to see what you can come up with. Break things and rebuilt them again. This is the best way to learn.
Shameless plug time
I have a new course called Hello Rails. Hello Rails is modern course designed to help you start using and understanding Ruby on Rails fast. If you're a novice when it comes to Ruby or Ruby on Rails I invite you to check out the site. The course will be much like these builds but a super more in-depth version with more realistic goals and deliverables. View the course!