54
loading...
This website collects cookies to deliver better user experience
# if the gem doesn't yet exist in your ruby application:
gem install rails
# generally run first thing, to install all dependencies
bundle install
# if you modify your gem file and need to update
# your installed dependencies
bundle update
# creates a new rails application in the current
# directory called ingenious_app
rails new ingenious_app
# lists the current version of rails being used
rails version
# rails -v
# runs a web server so that you may access the database
# using the default url http://localhost:3000
rails server
# rails s
# to exit, use Ctrl-C
# opens a console session to play/work with the data,
# test various ideas, check your work, etc.
rails console
# rails c
# to exit, use Ctrl-D
# lists all active routes that exist in the rails application
rails routes
# generates new code for you - more on this command in a bit
rails generate
# rails g
# produces an abbreviated list of available commands,
# most lack any description
rails -h
# add -h to any command to learn more about possible options
rails <command> -h
# example:
# rails generate -h
# produces a detailed list of available commands
# with descriptions
rails --tasks
# creates the database for the current environment (ex: development)
rails db:create
# deletes the database for the current environment
rails db:drop
# runs any pending migrations for the current environment
rails db:migrate
# checks the status of all migrations (up, down, or pending)
rails db:migrate:status
# rolls back (undo) the most recent migration
rails db:rollback
# runs the db/seed.rb file, thereby seeding the database
rails db:seed
# truncates all tables in the current database, re-runs the
# seed file (empties the database, replaces with seed data)
rails db:seed:replant
# runs multiple commands (db:drop and db:setup); resets
# database by deleting database, loading current schema into
# current environment database, then running seed file
rails db:reset
# generates a User model where relationships,
# custom methods, and validations can be defined.
# Specify desired columns and their data types. A column's
# data type defaults to string unless otherwise specified
rails g model user name age:integer
# creates the following User model
class User < ApplicationRecord
end
# as well as the following migration file that creates
# the Users table with columns for name and age.
class CreateUsers <ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :name
t.integer :age
t.timestamps
end
end
end
# Generates a UsersController where CRUD route methods
# and supporting private methods are defined
rails g controller users index show
# Any methods you list after the controllers name pre-build
# them for you. The above creates the following:
class UsersController < ApplicationController
def index
end
def show
end
end
# Generates a UserSerializer
rails g serializer user name age
# creates the following:
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :age
end
# creates a migration file that will need filling in
rails g migration create_users name age:integer
# creates the following migration;
# note that including columns does nothing:
class CreateUsers <ActiveRecord::Migration[6.1]
def change
end
end
# creates a migration with create_table and columns
# defined, the users controller, the user model,
# AND the user serializer. Woah!
rails g resource user name age:integer