2012-11-29 08:32:12 -05:00
The Rails Command Line
======================
2009-02-05 20:57:02 -05:00
2012-11-29 17:25:02 -05:00
After reading this guide, you will know:
2012-12-07 12:50:09 -05:00
* How to create a Rails application.
* How to generate models, controllers, database migrations, and unit tests.
* How to start a development server.
* How to experiment with objects through an interactive shell.
* How to profile and benchmark your new creation.
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
--------------------------------------------------------------------------------
2009-02-05 20:57:02 -05:00
2012-09-02 01:08:20 -04:00
NOTE: This tutorial assumes you have basic Rails knowledge from reading the [Getting Started with Rails Guide ](getting_started.html ).
2011-04-13 17:19:47 -04:00
2012-09-01 17:25:58 -04:00
Command Line Basics
-------------------
2009-02-05 20:57:02 -05:00
There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:
2012-09-01 19:34:21 -04:00
* `rails console`
* `rails server`
* `rake`
* `rails generate`
* `rails dbconsole`
* `rails new app_name`
2009-02-05 20:57:02 -05:00
2013-06-13 14:33:45 -04:00
All commands can run with ```-h or --help``` to list more information.
2009-02-05 20:57:02 -05:00
Let's create a simple Rails application to step through each of these commands in context.
2012-09-01 21:37:59 -04:00
### `rails new`
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
The first thing we'll want to do is create a new Rails application by running the `rails new` command after installing Rails.
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
INFO: You can install the rails gem by typing `gem install rails` , if you don't have it already.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-07-30 12:19:01 -04:00
$ rails new commandsapp
2010-08-14 01:13:00 -04:00
create
2011-12-11 12:47:54 -05:00
create README.rdoc
2010-03-11 06:12:04 -05:00
create Rakefile
create config.ru
2012-05-28 02:47:48 -04:00
create .gitignore
2010-03-11 06:12:04 -05:00
create Gemfile
create app
2009-02-05 20:57:02 -05:00
...
2010-03-11 06:12:04 -05:00
create tmp/cache
2012-05-28 02:47:48 -04:00
...
run bundle install
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.
2012-09-01 21:37:59 -04:00
### `rails server`
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
The `rails server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to access your application through a web browser.
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
With no further work, `rails server` will run our new shiny Rails app:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2009-02-05 20:57:02 -05:00
$ cd commandsapp
2010-02-06 11:18:10 -05:00
$ rails server
2010-04-19 11:35:59 -04:00
=> Booting WEBrick
2013-06-16 02:22:46 -04:00
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
2010-04-19 11:35:59 -04:00
=> Call with -d to detach
=> Ctrl-C to shutdown server
2013-08-06 16:51:00 -04:00
[2013-08-07 02:00:01] INFO WEBrick 1.3.1
[2013-08-07 02:00:01] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin11.2.0]
[2013-08-07 02:00:01] INFO WEBrick::HTTPServer#start: pid=69680 port=3000
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-02 01:08:20 -04:00
With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open [http://localhost:3000 ](http://localhost:3000 ), you will see a basic Rails app running.
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
INFO: You can also use the alias "s" to start the server: `rails s` .
2011-05-28 13:48:08 -04:00
2012-09-01 21:37:59 -04:00
The server can be run on a different port using the `-p` option. The default development environment can be changed using `-e` .
2011-06-11 16:46:08 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-06-11 18:32:01 -04:00
$ rails server -e production -p 4000
2012-09-01 17:08:06 -04:00
```
2011-06-11 16:46:08 -04:00
2013-03-23 15:04:06 -04:00
The `-b` option binds Rails to the specified IP, by default it is 0.0.0.0. You can run a server as a daemon by passing a `-d` option.
2011-11-28 10:33:01 -05:00
2012-09-01 21:37:59 -04:00
### `rails generate`
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
The `rails generate` command uses templates to create a whole lot of things. Running `rails generate` by itself gives a list of available generators:
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
INFO: You can also use the alias "g" to invoke the generator command: `rails g` .
2011-07-21 14:25:31 -04:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails generate
2011-06-11 16:00:40 -04:00
Usage: rails generate GENERATOR [args] [options]
2009-02-05 20:57:02 -05:00
...
...
2010-03-11 06:12:04 -05:00
Please choose a generator below.
2009-02-05 20:57:02 -05:00
2010-03-11 06:12:04 -05:00
Rails:
2012-05-28 02:47:48 -04:00
assets
2010-03-11 06:12:04 -05:00
controller
generator
...
...
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
NOTE: You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!
2012-09-06 22:26:59 -04:00
Using generators will save you a large amount of time by writing **boilerplate code** , code that is necessary for the app to work.
2009-02-05 20:57:02 -05:00
Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
2012-09-01 21:37:59 -04:00
INFO: All Rails console utilities have help text. As with most *nix utilities, you can try adding `--help` or `-h` to the end, for example `rails server --help` .
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails generate controller
2011-02-08 16:48:12 -05:00
Usage: rails generate controller NAME [action action] [options]
2009-02-05 20:57:02 -05:00
...
...
2012-05-28 02:47:48 -04:00
Description:
...
To create a controller within a module, specify the controller name as a
path like 'parent_module/controller_name'.
...
2009-02-05 20:57:02 -05:00
Example:
2012-05-28 02:47:48 -04:00
`rails generate controller CreditCard open debit credit close`
2009-02-05 20:57:02 -05:00
Credit card controller with URLs like /credit_card/debit.
2012-10-08 00:59:42 -04:00
Controller: app/controllers/credit_card_controller.rb
Test: test/controllers/credit_card_controller_test.rb
Views: app/views/credit_card/debit.html.erb [...]
Helper: app/helpers/credit_card_helper.rb
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-06 22:26:59 -04:00
The controller generator is expecting parameters in the form of `generate controller ControllerName action1 action2` . Let's make a `Greetings` controller with an action of **hello** , which will say something nice to us.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails generate controller Greetings hello
2009-02-05 20:57:02 -05:00
create app/controllers/greetings_controller.rb
2010-12-15 15:18:11 -05:00
route get "greetings/hello"
2010-03-11 06:12:04 -05:00
invoke erb
create app/views/greetings
create app/views/greetings/hello.html.erb
2011-02-08 16:48:12 -05:00
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/controllers/greetings_controller_test.rb
2010-03-11 06:12:04 -05:00
invoke helper
create app/helpers/greetings_helper.rb
2011-02-08 16:48:12 -05:00
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/helpers/greetings_helper_test.rb
2011-04-14 09:04:10 -04:00
invoke assets
2012-05-28 02:47:48 -04:00
invoke coffee
create app/assets/javascripts/greetings.js.coffee
invoke scss
create app/assets/stylesheets/greetings.css.scss
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-08-02 14:20:49 -04:00
What all did this generate? It made sure a bunch of directories were in our application, and created a controller file, a view file, a functional test file, a helper for the view, a JavaScript file and a stylesheet file.
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
Check out the controller and modify it a little (in `app/controllers/greetings_controller.rb` ):
2009-02-05 20:57:02 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2009-02-24 07:29:25 -05:00
class GreetingsController < ApplicationController
2009-02-05 20:57:02 -05:00
def hello
2010-03-11 06:12:04 -05:00
@message = "Hello, how are you today?"
2009-02-05 20:57:02 -05:00
end
end
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
Then the view, to display our message (in `app/views/greetings/hello.html.erb` ):
2009-02-05 20:57:02 -05:00
2012-09-03 21:21:24 -04:00
```erb
2009-02-05 20:57:02 -05:00
< h1 > A Greeting for You!< / h1 >
< p > < %= @message %></ p >
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
Fire up your server using `rails server` .
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails server
2009-02-05 20:57:02 -05:00
=> Booting WEBrick...
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-02 01:08:20 -04:00
The URL will be [http://localhost:3000/greetings/hello ](http://localhost:3000/greetings/hello ).
2009-02-05 20:57:02 -05:00
2012-09-06 22:26:59 -04:00
INFO: With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit the **index** action of that controller.
2009-02-05 20:57:02 -05:00
2011-06-16 13:18:05 -04:00
Rails comes with a generator for data models too.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails generate model
2012-05-28 02:47:48 -04:00
Usage:
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
2009-02-05 20:57:02 -05:00
...
2013-04-30 11:59:12 -04:00
Active Record options:
2012-05-28 02:47:48 -04:00
[--migration] # Indicates when to generate migration
# Default: true
2009-02-05 20:57:02 -05:00
2012-05-28 02:47:48 -04:00
...
2009-02-05 20:57:02 -05:00
2012-05-28 02:47:48 -04:00
Description:
Create rails files for model generator.
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-02 01:08:20 -04:00
NOTE: For a list of available field types, refer to the [API documentation ](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column ) for the column method for the `TableDefinition` class.
2010-12-08 20:45:36 -05:00
2012-09-06 22:26:59 -04:00
But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A **scaffold** in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
2009-02-05 20:57:02 -05:00
2010-03-11 06:12:04 -05:00
We will set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails generate scaffold HighScore game:string score:integer
2012-05-28 02:47:48 -04:00
invoke active_record
2013-07-17 11:47:08 -04:00
create db/migrate/20130717151933_create_high_scores.rb
2009-02-05 20:57:02 -05:00
create app/models/high_score.rb
2012-05-28 02:47:48 -04:00
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/models/high_score_test.rb
2012-05-28 02:47:48 -04:00
create test/fixtures/high_scores.yml
2013-01-03 15:14:24 -05:00
invoke resource_route
route resources :high_scores
2012-05-28 02:47:48 -04:00
invoke scaffold_controller
create app/controllers/high_scores_controller.rb
invoke erb
create app/views/high_scores
create app/views/high_scores/index.html.erb
create app/views/high_scores/edit.html.erb
create app/views/high_scores/show.html.erb
create app/views/high_scores/new.html.erb
create app/views/high_scores/_form.html.erb
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/controllers/high_scores_controller_test.rb
2012-05-28 02:47:48 -04:00
invoke helper
create app/helpers/high_scores_helper.rb
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/helpers/high_scores_helper_test.rb
2013-07-17 11:47:08 -04:00
invoke jbuilder
create app/views/high_scores/index.json.jbuilder
create app/views/high_scores/show.json.jbuilder
2012-05-28 02:47:48 -04:00
invoke assets
invoke coffee
create app/assets/javascripts/high_scores.js.coffee
invoke scss
create app/assets/stylesheets/high_scores.css.scss
invoke scss
2013-07-17 11:47:08 -04:00
identical app/assets/stylesheets/scaffolds.css.scss
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-06 22:26:59 -04:00
The generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the `high_scores` table and fields), takes care of the route for the **resource** , and new tests for everything.
2009-02-05 20:57:02 -05:00
2013-07-17 11:47:08 -04:00
The migration requires that we **migrate** , that is, run some Ruby code (living in that `20130717151933_create_high_scores.rb` ) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the `rake db:migrate` command. We'll talk more about Rake in-depth in a little while.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2009-02-05 20:57:02 -05:00
$ rake db:migrate
2010-03-11 06:12:04 -05:00
== CreateHighScores: migrating ===============================================
-- create_table(:high_scores)
2012-05-28 02:47:48 -04:00
-> 0.0017s
== CreateHighScores: migrated (0.0019s) ======================================
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
INFO: Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.
2010-04-08 10:38:26 -04:00
Let's see the interface Rails created for us.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-04-08 10:38:26 -04:00
$ rails server
2012-09-01 17:08:06 -04:00
```
2010-04-08 10:38:26 -04:00
2012-09-02 01:08:20 -04:00
Go to your browser and open [http://localhost:3000/high_scores ](http://localhost:3000/high_scores ), now we can create new high scores (55,160 on Space Invaders!)
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
### `rails console`
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
The `console` command lets you interact with your Rails application from the command line. On the underside, `rails console` uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
INFO: You can also use the alias "c" to invoke the console: `rails c` .
2011-05-28 13:48:08 -04:00
2012-09-01 21:37:59 -04:00
You can specify the environment in which the `console` command should operate.
2012-03-24 18:17:21 -04:00
2012-09-01 20:45:26 -04:00
```bash
2012-05-28 02:47:48 -04:00
$ rails console staging
2012-09-01 17:08:06 -04:00
```
2012-03-24 18:17:21 -04:00
2012-09-01 21:37:59 -04:00
If you wish to test out some code without changing any data, you can do that by invoking `rails console --sandbox` .
2010-12-15 15:18:11 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-12-15 15:18:11 -05:00
$ rails console --sandbox
2013-06-16 02:22:46 -04:00
Loading development environment in sandbox (Rails 4.0.0)
2010-12-15 15:18:11 -05:00
Any modifications you make will be rolled back on exit
irb(main):001:0>
2012-09-01 17:08:06 -04:00
```
2010-12-15 15:18:11 -05:00
2012-09-01 21:37:59 -04:00
### `rails dbconsole`
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
`rails dbconsole` figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
INFO: You can also use the alias "db" to invoke the dbconsole: `rails db` .
2011-05-28 13:48:08 -04:00
2012-09-01 21:37:59 -04:00
### `rails runner`
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
`runner` runs Ruby code in the context of Rails non-interactively. For instance:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails runner "Model.long_running_method"
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
INFO: You can also use the alias "r" to invoke the runner: `rails r` .
2011-07-21 14:16:42 -04:00
2012-09-01 21:37:59 -04:00
You can specify the environment in which the `runner` command should operate using the `-e` switch.
2011-06-11 12:41:16 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-06-11 12:41:16 -04:00
$ rails runner -e staging "Model.long_running_method"
2012-09-01 17:08:06 -04:00
```
2011-06-11 12:41:16 -04:00
2012-09-01 21:37:59 -04:00
### `rails destroy`
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
Think of `destroy` as the opposite of `generate` . It'll figure out what generate did, and undo it.
2009-02-05 20:57:02 -05:00
2012-09-01 19:34:21 -04:00
INFO: You can also use the alias "d" to invoke the destroy command: `rails d` .
2011-08-20 18:25:11 -04:00
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails generate model Oops
2012-05-28 02:47:48 -04:00
invoke active_record
create db/migrate/20120528062523_create_oops.rb
create app/models/oops.rb
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/models/oops_test.rb
2012-05-28 02:47:48 -04:00
create test/fixtures/oops.yml
2012-09-01 17:08:06 -04:00
```
2012-09-01 20:45:26 -04:00
```bash
2010-02-06 11:18:10 -05:00
$ rails destroy model Oops
2012-05-28 02:47:48 -04:00
invoke active_record
remove db/migrate/20120528062523_create_oops.rb
remove app/models/oops.rb
invoke test_unit
2012-10-08 00:59:42 -04:00
remove test/models/oops_test.rb
2012-05-28 02:47:48 -04:00
remove test/fixtures/oops.yml
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
Rake
----
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and `.rake` files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing `rake --tasks` . Each task has a description, and should help you find the thing you need.
2011-06-06 15:30:07 -04:00
2013-06-13 14:33:45 -04:00
To get the full backtrace for running rake task you can pass the option
```--trace``` to command line, for example ```rake db:create --trace```.
2012-09-01 20:45:26 -04:00
```bash
2011-06-06 15:30:07 -04:00
$ rake --tasks
2012-05-28 02:47:48 -04:00
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake db:create # Create the database from config/database.yml for the current Rails.env
2011-06-06 15:30:07 -04:00
...
2013-01-16 13:27:06 -05:00
rake log:clear # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
2012-05-28 02:47:48 -04:00
rake middleware # Prints out your Rack middleware stack
2011-06-06 15:30:07 -04:00
...
2012-05-28 02:47:48 -04:00
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
2012-09-01 17:08:06 -04:00
```
2013-06-13 14:33:45 -04:00
INFO: You can also use ```rake -T``` to get the list of tasks.
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
### `about`
2011-06-06 15:30:07 -04:00
2012-09-01 19:34:21 -04:00
`rake about` gives information about version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version. It is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2010-04-19 11:30:58 -04:00
$ rake about
2009-02-05 20:57:02 -05:00
About your application's environment
2011-12-20 12:34:11 -05:00
Ruby version 1.9.3 (x86_64-linux)
2010-04-19 11:30:58 -04:00
RubyGems version 1.3.6
2011-10-22 06:24:55 -04:00
Rack version 1.3
2013-10-09 08:44:54 -04:00
Rails version 4.1.0
2011-08-05 16:09:05 -04:00
JavaScript Runtime Node.js (V8)
2013-10-09 08:44:54 -04:00
Active Record version 4.1.0
Action Pack version 4.1.0
Action View version 4.1.0
Action Mailer version 4.1.0
Active Support version 4.1.0
2013-09-13 13:54:56 -04:00
Middleware Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #< ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd131a7c88 > , Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
2010-04-19 11:34:42 -04:00
Application root /home/foobar/commandsapp
2009-02-05 20:57:02 -05:00
Environment development
2011-07-31 12:28:59 -04:00
Database adapter sqlite3
2011-08-05 16:09:05 -04:00
Database schema version 20110805173523
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-01 21:37:59 -04:00
### `assets`
2011-06-06 15:30:07 -04:00
2012-09-01 19:34:21 -04:00
You can precompile the assets in `app/assets` using `rake assets:precompile` and remove those compiled assets using `rake assets:clean` .
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
### `db`
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
The most common tasks of the `db:` Rake namespace are `migrate` and `create` , and it will pay off to try out all of the migration rake tasks (`up`, `down` , `redo` , `reset` ). `rake db:version` is useful when troubleshooting, telling you the current version of the database.
2011-06-06 15:30:07 -04:00
2012-09-02 01:08:20 -04:00
More information about migrations can be found in the [Migrations ](migrations.html ) guide.
2011-06-11 12:41:16 -04:00
2012-09-01 21:37:59 -04:00
### `doc`
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
The `doc:` namespace has the tools to generate documentation for your app, API documentation, guides. Documentation can also be stripped which is mainly useful for slimming your codebase, like if you're writing a Rails application for an embedded platform.
2011-06-16 02:59:47 -04:00
2012-09-01 21:37:59 -04:00
* `rake doc:app` generates documentation for your application in `doc/app` .
* `rake doc:guides` generates Rails guides in `doc/guides` .
* `rake doc:rails` generates API documentation for Rails in `doc/api` .
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
### `notes`
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
`rake notes` will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension `.builder` , `.rb` , `.erb` , `.haml` and `.slim` for both default and custom annotations.
2011-06-15 13:23:26 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-06-15 13:23:26 -04:00
$ rake notes
(in /home/foobar/commandsapp)
app/controllers/admin/users_controller.rb:
* [ 20] [TODO] any other way to do this?
* [132] [FIXME] high priority for next deploy
2013-03-24 02:59:33 -04:00
app/models/school.rb:
2011-06-15 13:23:26 -04:00
* [ 13] [OPTIMIZE] refactor this code to make it faster
* [ 17] [FIXME]
2012-09-01 17:08:06 -04:00
```
2011-06-15 13:23:26 -04:00
2012-09-01 21:37:59 -04:00
If you are looking for a specific annotation, say FIXME, you can use `rake notes:fixme` . Note that you have to lower case the annotation's name.
2011-06-15 13:56:19 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-06-15 13:56:19 -04:00
$ rake notes:fixme
(in /home/foobar/commandsapp)
app/controllers/admin/users_controller.rb:
* [132] high priority for next deploy
2013-03-24 02:59:33 -04:00
app/models/school.rb:
2011-06-15 13:56:19 -04:00
* [ 17]
2012-09-01 17:08:06 -04:00
```
2011-06-15 13:56:19 -04:00
2012-09-01 21:37:59 -04:00
You can also use custom annotations in your code and list them using `rake notes:custom` by specifying the annotation using an environment variable `ANNOTATION` .
2011-06-15 13:23:26 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-06-15 13:23:26 -04:00
$ rake notes:custom ANNOTATION=BUG
(in /home/foobar/commandsapp)
2013-03-24 02:59:33 -04:00
app/models/post.rb:
2011-06-15 13:23:26 -04:00
* [ 23] Have to fix this one before pushing!
2012-09-01 17:08:06 -04:00
```
2011-06-06 15:30:07 -04:00
2011-06-15 13:56:19 -04:00
NOTE. When using specific annotations and custom annotations, the annotation name (FIXME, BUG etc) is not displayed in the output lines.
2013-01-09 16:59:07 -05:00
By default, `rake notes` will look in the `app` , `config` , `lib` , `bin` and `test` directories. If you would like to search other directories, you can provide them as a comma separated list in an environment variable `SOURCE_ANNOTATION_DIRECTORIES` .
2012-04-30 01:18:27 -04:00
2012-09-01 20:45:26 -04:00
```bash
2013-04-03 11:14:35 -04:00
$ export SOURCE_ANNOTATION_DIRECTORIES='spec,vendor'
2012-04-30 01:18:27 -04:00
$ rake notes
(in /home/foobar/commandsapp)
2013-03-24 02:59:33 -04:00
app/models/user.rb:
2012-04-30 01:18:27 -04:00
* [ 35] [FIXME] User should have a subscription at this point
2013-04-03 11:14:35 -04:00
spec/models/user_spec.rb:
2012-04-30 01:18:27 -04:00
* [122] [TODO] Verify the user that has a subscription works
2012-09-01 17:08:06 -04:00
```
2012-04-30 01:18:27 -04:00
2012-09-01 21:37:59 -04:00
### `routes`
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
`rake routes` will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
### `test`
2011-06-06 15:30:07 -04:00
2012-09-02 01:08:20 -04:00
INFO: A good description of unit testing in Rails is given in [A Guide to Testing Rails Applications ](testing.html )
2011-06-06 15:30:07 -04:00
2013-08-26 18:36:03 -04:00
Rails comes with a test suite called Minitest. Rails owes its stability to the use of tests. The tasks available in the `test:` namespace helps in running the different tests you will hopefully write.
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
### `tmp`
2011-06-06 15:30:07 -04:00
2012-09-01 19:34:21 -04:00
The `Rails.root/tmp` directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for files), process id files, and cached actions.
2011-08-05 16:09:05 -04:00
2013-06-23 03:59:37 -04:00
The `tmp:` namespaced tasks will help you clear and create the `Rails.root/tmp` directory:
2011-08-05 16:09:05 -04:00
2012-09-01 21:37:59 -04:00
* `rake tmp:cache:clear` clears `tmp/cache` .
* `rake tmp:sessions:clear` clears `tmp/sessions` .
* `rake tmp:sockets:clear` clears `tmp/sockets` .
* `rake tmp:clear` clears all the three: cache, sessions and sockets.
2013-06-23 03:59:37 -04:00
* `rake tmp:create` creates tmp directories for sessions, cache, sockets, and pids.
2011-06-06 15:30:07 -04:00
2012-09-01 17:25:58 -04:00
### Miscellaneous
2011-06-06 15:30:07 -04:00
2012-09-01 21:37:59 -04:00
* `rake stats` is great for looking at statistics on your code, displaying things like KLOCs (thousands of lines of code) and your code to test ratio.
* `rake secret` will give you a pseudo-random key to use for your session secret.
2012-09-01 19:34:21 -04:00
* `rake time:zones:all` lists all the timezones Rails knows about.
2011-06-06 15:30:07 -04:00
2012-09-28 14:17:15 -04:00
### Custom Rake Tasks
2012-07-30 19:15:06 -04:00
2013-09-09 06:11:28 -04:00
Custom rake tasks have a `.rake` extension and are placed in
2013-09-12 18:08:24 -04:00
`Rails.root/lib/tasks` . You can create these custom rake tasks with the
`bin/rails generate task` command.
2012-07-30 19:15:06 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-07-30 19:15:06 -04:00
desc "I am short, but comprehensive description for my cool task"
2012-11-15 16:52:54 -05:00
task task_name: [:prerequisite_task, :another_task_we_depend_on] do
2012-09-20 21:40:49 -04:00
# All your magic here
2012-07-30 19:15:06 -04:00
# Any valid Ruby code is allowed
end
2012-09-01 17:08:06 -04:00
```
2012-07-30 19:15:06 -04:00
2012-09-28 14:17:15 -04:00
To pass arguments to your custom rake task:
2012-07-30 19:15:06 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-07-30 19:15:06 -04:00
task :task_name, [:arg_1] => [:pre_1, :pre_2] do |t, args|
# You can use args from here
end
2012-09-01 17:08:06 -04:00
```
2012-07-30 19:15:06 -04:00
You can group tasks by placing them in namespaces:
2012-09-01 17:08:06 -04:00
```ruby
2012-09-28 15:08:16 -04:00
namespace :db do
2012-07-30 19:15:06 -04:00
desc "This task does nothing"
task :nothing do
# Seriously, nothing
end
end
2012-09-01 17:08:06 -04:00
```
2012-07-30 19:15:06 -04:00
2012-09-28 14:17:15 -04:00
Invocation of the tasks will look like:
2012-07-30 19:15:06 -04:00
2012-09-01 20:45:26 -04:00
```bash
2012-07-30 19:15:06 -04:00
rake task_name
rake "task_name[value 1]" # entire argument string should be quoted
2012-09-28 14:17:15 -04:00
rake db:nothing
2012-09-28 15:08:16 -04:00
```
2012-07-30 19:15:06 -04:00
2012-09-01 21:37:59 -04:00
NOTE: If your need to interact with your application models, perform database queries and so on, your task should depend on the `environment` task, which will load your application code.
2012-07-30 19:15:06 -04:00
2012-09-01 17:25:58 -04:00
The Rails Advanced Command Line
-------------------------------
2009-02-05 20:57:02 -05:00
2010-12-15 15:18:11 -05:00
More advanced use of the command line is focused around finding useful (even surprising at times) options in the utilities, and fitting those to your needs and specific work flow. Listed here are some tricks up Rails' sleeve.
2009-02-05 20:57:02 -05:00
2012-09-01 17:25:58 -04:00
### Rails with Databases and SCM
2009-02-05 20:57:02 -05:00
When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes.
2012-09-01 21:37:59 -04:00
Let's see what a `--git` option and a `--database=postgresql` option will do for us:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2009-02-05 20:57:02 -05:00
$ mkdir gitapp
$ cd gitapp
$ git init
Initialized empty Git repository in .git/
2010-07-30 12:19:01 -04:00
$ rails new . --git --database=postgresql
2009-02-05 20:57:02 -05:00
exists
create app/controllers
create app/helpers
...
...
create tmp/cache
create tmp/pids
create Rakefile
add 'Rakefile'
2011-12-11 12:47:54 -05:00
create README.rdoc
add 'README.rdoc'
2011-02-10 13:07:55 -05:00
create app/controllers/application_controller.rb
add 'app/controllers/application_controller.rb'
2009-02-05 20:57:02 -05:00
create app/helpers/application_helper.rb
...
create log/test.log
add 'log/test.log'
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2012-09-06 22:26:59 -04:00
We had to create the **gitapp** directory and initialize an empty git repository before Rails would add files it created to our repository. Let's see what it put in our database configuration:
2009-02-05 20:57:02 -05:00
2012-09-01 20:45:26 -04:00
```bash
2009-02-05 20:57:02 -05:00
$ cat config/database.yml
2011-07-05 13:43:34 -04:00
# PostgreSQL. Versions 8.2 and up are supported.
2009-02-05 20:57:02 -05:00
#
2013-01-04 00:24:46 -05:00
# Install the pg driver:
# gem install pg
# On OS X with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
2009-02-05 20:57:02 -05:00
# On Windows:
2013-01-04 00:24:46 -05:00
# gem install pg
2009-02-05 20:57:02 -05:00
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
2013-01-04 00:24:46 -05:00
#
# Configure Using Gemfile
# gem 'pg'
#
2009-02-05 20:57:02 -05:00
development:
adapter: postgresql
encoding: unicode
database: gitapp_development
2012-03-07 16:46:06 -05:00
pool: 5
2009-02-05 20:57:02 -05:00
username: gitapp
password:
...
...
2012-09-01 17:08:06 -04:00
```
2009-02-05 20:57:02 -05:00
2011-06-11 12:41:16 -04:00
It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database.
2012-09-01 21:37:59 -04:00
NOTE. The only catch with using the SCM options is that you have to make your application's directory first, then initialize your SCM, then you can run the `rails new` command to generate the basis of your app.