rails console
rails server
rails test
rails generate
rails db:migrate
rails db:create
rails routes
rails dbconsole
rails new app_name
rails --help
. Each command has a description, and should help you find the thing you need.rails new
rails new
command after installing Rails.gem install rails
, if you don't have it already.rails server
rails server
command launches a web server named Puma which comes bundled with Rails. You'll use this any time you want to access your application through a web browser.rails server
will run our new shiny Rails app:rails s
.-p
option. The default development environment can be changed using -e
.-b
option binds Rails to the specified IP, by default it is localhost. You can run a server as a daemon by passing a -d
option.rails generate
rails generate
command uses templates to create a whole lot of things. Running rails generate
by itself gives a list of available generators:rails g
.--help
or -h
to the end, for example rails server --help
.generate controller ControllerName action1 action2
. Let's make a Greetings
controller with an action of hello, which will say something nice to us.app/controllers/greetings_controller.rb
):app/views/greetings/hello.html.erb
):rails server
.type
parameter, refer to the API documentation for the add_column method for the SchemaStatements
module. The index
parameter generates a corresponding index for the column.high_scores
table and fields), takes care of the route for the resource, and new tests for everything.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 rails db:migrate
command. We'll talk more about that command below.rails console
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.rails c
.console
command should operate.rails console --sandbox
.rails console
you have access to the app
and helper
instances.app
method you can access named route helpers, as well as do requests.helper
method it is possible to access Rails and your application's helpers.rails dbconsole
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 (including MariaDB), PostgreSQL, and SQLite3.rails db
.rails runner
runner
runs Ruby code in the context of Rails non-interactively. For instance:rails r
.runner
command should operate using the -e
switch.rails destroy
destroy
as the opposite of generate
. It'll figure out what generate did, and undo it.rails d
.rails about
rails 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.rails assets:
app/assets
using rails assets:precompile
, and remove older compiled assets using rails assets:clean
. The assets:clean
command allows for rolling deploys that may still be linking to an old asset while the new assets are being built.public/assets
completely, you can use rails assets:clobber
.rails db:
db:
rails namespace are migrate
and create
, and it will pay off to try out all of the migration rails commands (up
, down
, redo
, reset
). rails db:version
is useful when troubleshooting, telling you the current version of the database.rails notes
rails notes
searches through your code for comments beginning with a specific keyword. You can refer to rails notes --help
for information about usage.app
, config
, db
, lib
, and test
directories for FIXME, OPTIMIZE, and TODO annotations in files with extension .builder
, .rb
, .rake
, .yml
, .yaml
, .ruby
, .css
, .js
, and .erb
.--annotations
argument. By default, it will search for FIXME, OPTIMIZE, and TODO.Note that annotations are case sensitive.config.annotations.register_tags
. It receives a list of tags.config.annotations.register_directories
. It receives a list of directory names.config.annotations.register_extensions
. It receives a list of extensions with its corresponding regex to match it up.rails routes
rails 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.rails test
test:
namespace helps in running the different tests you will hopefully write.rails tmp:
Rails.root/tmp
directory is, like the *nix /tmp directory, the holding place for temporary files like process id files and cached actions.tmp:
namespaced commands will help you clear and create the Rails.root/tmp
directory:rails tmp:cache:clear
clears tmp/cache
.rails tmp:sockets:clear
clears tmp/sockets
.rails tmp:screenshots:clear
clears tmp/screenshots
.rails tmp:clear
clears all cache, sockets, and screenshot files.rails tmp:create
creates tmp directories for cache, sockets, and pids.rails 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.rails secret
will give you a pseudo-random key to use for your session secret.rails time:zones:all
lists all the timezones Rails knows about..rake
extension and are placed inRails.root/lib/tasks
. You can create these custom rake tasks with therails generate task
command.environment
task, which will load your application code.--git
option and a --database=postgresql
option will do for us:database.yml
configuration corresponding to our choice of PostgreSQL for database.rails new
command to generate the basis of your app.