Ruby rails: usage

From wikinotes
Revision as of 22:18, 16 November 2020 by Will (talk | contribs) (→‎Basics)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Rails

Documentation

rails commandline docs https://guides.rubyonrails.org/v4.1/command_line.html#rails-dbconsole

Basics

rails new -h          # subparser help
rails --version       # print rails version

rails server          # start server (default) http://localhost:3000
rails console         # pry console with rails
rails runner file.rb  # run file in rails console
load 'file.rb'        # (within pry)

Generators

# create project blog
rails new Blog

# create 'WelcomeController.index' 
# ('Welcome' controller, with action 'index')
rails generate controller Welcome index

# create model, db-migration file to create db-table
rails generate model Article \           
  title:string text:text

# create db-migration
rails generate migration YourMigrationName

Tasks

all rails tasks https://jacopretorius.net/2014/02/all-rails-db-rake-tasks-and-what-they-do.html
rails db:migrate                            # run all un-run migrations
rails db:rollback                           # roll back the last migration

Testing

rails test              # run all tests
rails test file.rb      # run tests in file
rails test file.rb:123  # line num in file
rails test -n /foo/     # run tests matching 'foo'