If you’ve worked with a Rails application, you’ve used the
rails command. In this post, we’re going to go in depth with the rails command and find out what it does. For the purposes of this post, placeholder text will go in the form of
rails command [value] where
[value] represents something you’re trying to accomplish. For example, if we’re generating a new rails command called
rails new [value] would be equal to saying
rails new treehouse. With that out of the way, let’s get going!
Generating a New Application
In order to generate a rails application, you’ll generally use the
rails new [app name] command. This is pretty straightforward but did you know that there are a ton of different options that you can possibly use? The format is as follows:
rails new APP_PATH [options]
APP_PATH is going to be the path to the new app. Generally, you will say that it is relative to the current directory. However, a full path can be specified such as:
rails new /Users/jason/Sites/treehouse
There are a ton of different options that go along with it. It’s possible to specify a different Ruby installation, skip using bundler, and more. Let’s take a look at some of the most common ones you might use.
The template option allows you to run different templates against a freshly generated rails application. The flag is taken with a path as an argument to the template file. You can read more about Rails application templates at the rails guide on the subject. The railsapps project has a ton of different templates you can peruse to see if any apply to your applications.
This flag allows the database to be set when creating a rails application. The defaults for the selected database will be written to the
config/database.yml template. If no argument is specified, the default database used is sqlite3. This keeps the databases themselves in the
db directory. While generally fine in development, sqlite3 is rarely used in production.
If you aren’t interested in using the Rails asset pipeline, this can be skipped entirely when creating a new rails application. This might be done in the cases of building an API only application or using grunt or bower to manage assets.
Runtime Options for All Commands
These are going to be the most often used of the bunch. When you run a rails generate command, you can almost always use the following options to modify what the command does. There are two that are used most:
--force option to overwrite files. You may want to do this in the case of regenerating a model, controller, view or the like.
When you run the generate command (aliased as
g) with no arguments, rails will give you the option to generate any of the following:
As you might expect from that list, you can go further in depth on each of those. When generating any of the above items, such as a model, the runtime options above can be used. So doing the following:
rails generate model account
Would generate the following files:
app/models/account.rb test/models/account_test.rb test/fixtures/accounts.yml db/migrate/XXX_create_accounts.rb
However, the files would not be generated if you modified the above command to be the following:
rails generate model account -p
In that case, rails would only pretend to generate the files without actually overwriting anything.
You can ask for help and options on any of those generators by using the
This is a quick command. Almost anything created with the
generate command can be destroyed with the
destroy command. For example, to destroy the above created account model:
rails destroy model account -p
-p flag to pretend to delete something first. Just in case.
The rails console allows you to launch an
irb session with your rails application loaded. This means you have access to all of your models, controllers, gems, etc. There are a couple of flags which make the rails console extremely useful.
--environment option allows you to load in a different rails environment to the console. This will most likely take effect with a database selection where you can check out data in the context of that environment. For example, on a staging machine, you can launch a staging console by doing the following:
rails console -e staging
-e can also be omitted if you choose.
The sandbox option puts everything done in that console session in a database transaction. The transaction is then rolled back when you exit the console. So, feel free to delete your entire user database while you’re in the console and it will safely rollback on exit.
I’m just kidding, there are risks involved but it is a nice feature. Always keep backups and never do that in production.
The rails server command will run your application. The server command is run in the following format:
rails server [server] [options]
Unlike the generate command, the rails server does not allow a pretend or force flag.
If using a different server than webrick, that must be specified in your
Gemfile and available to your rails environment. For example, if running the puma server, the command would be the following:
rails server puma
The following options are valid:
-bBinding (ip address)
-cConfig file (for custom rack configuration)
-eChange the environment (defaults to development)
-PSpecify a PID file
We can combine the above options. For example, to bind to a local ip on port 8081 with the puma server, we would run the following command:
rails server puma -p 8081 -b 127.0.0.1
rails runner command executes rails code that is passed in as a string. It is not advisable to put a ton of code in here. Generally, you will want to keep this to one or two commands. For example, let’s say you had a class that billed subscriptions called
User with a class method called
report that printed a small summary of usage to standard output. In that case, you may want to do something like the following:
rails runner -e production 'User.report'
The rails runner can also point to a file. This will preload the app before running the contents of the Ruby file. In that case, you would give it a path:
rails runner -e production /home/deploy/apps/myapp/user_report.rb
Finally, you can use the rails runner as a shebang line to run executables.
But Wait, There’s More!
There’s one more command that’s a bit beyond the scope of this article, and that’s the
plugin command. That will generate a new plugin in your rails application in its own directory. Plugins can be used to enhance the behavior of your application and be reused relatively easily.
That covers all of the options to the rails command. The next section contains a list of tables with the different commands, their shortcuts, and a quick description. You can get any of these yourself by typing
rails help [command].
|rails new commands|
|Option (short)||Option (long)||Description|
||Path to Ruby binary. This defaults to the current Ruby.|
||Path to some application template (can be a filesystem path or URL)|
||Do or Don’t create a Gemfile|
||Do or Don’t run bundle install|
||Skip creating .gitignore file|
||Skip source control .keep files|
||Skip Active Record files|
||Skip Sprockets files|
||Don’t install Spring application preloader|
||Preconfigure for selected database (options: mysql, oracle, postgresql, sqlite3, frontbase, ibm_db, sqlserver, jdbcmysql, jdbcsqlite3, jdbcpostgresql, jdbc. Defaults to sqlite3.|
||Setup the application with Gemfile pointing to your Rails checkout|
||Setup the application with Gemfile pointing to Rails repository|
||Skip turbolinks gem|
||Skip Test::Unit files|
||Path to file containing extra configuration options for rails command|
||Skip loading of extra configuration options from .railsrc file|
||Generate new code (models, controllers, views, tests, etc)|
||Start a console|
||Start a server|
||Sandbox and rollback database modifications on exit.|
|Option (short)||Option (full)||Description|
||Overwrite files that already exist|
||Run but do not make any changes|
||Suppress status output|
||Skip files that already exist|
|rails server options|
|Flag (short)||Flag (long)||Description|
||Run server on specified port|
||Binds Rails to the specified IP. Default: localhost|
||Users a custom rack configuration|
||Runs server as a Daemon.|
||Enables the debugger.|
||Specify environment to run server under. Default: development|
||Specified PID file. Default: tmp/pids/server.pid|
|Rails Runner Options|
||Specifies the environment for the runner to operate under (test/development/production). Default: development|
||Show help message.|
Comments are closed.
Front End Web Design
User Experience (UX) Design
iOS Development with Swift
Python Web Development
Each is designed by our faculty of tech professionals to guide even a coding beginner to becoming a job-ready software developer armed with a professional portfolio of real-world projects. Try one out today with our free seven-day trial, and see if software development is for you.