Go-To Rails 명령줄 치트 시트!
Rails를 배우고 백엔드/전체 스택 기술 세트를 개발하는 동안 가장 일반적으로 사용되는 모든 명령을 나열하는 '치트 시트'를 만드는 것이 나와 다른 사람들에게 도움이 될 것이라고 생각했습니다. 나는 그것들을 다음과 같이 분류했다.
Getting Started
시작하기
# 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
기본 명령
Some ruby commands can be shortened. If an abbreviated command (or "shortcut alias") exists, it is listed as a comment right below the full command.
# 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
데이터베이스(db) 명령
# 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
생성기 명령
Generators were mentioned above in the basic commands section, but due to the robust nature of generators, they merit their own section. For these commands, it will be easier to explain using an example. We'll work with a Users model that requires a name and an age.
# 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
Note: The following command relies on having the active_model_serializers gem installed. Serializers allow you to define what data gets generated in a JSON using attributes and relationships.
# 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
Want something that does (almost) everything? Resource is the command you want!
# 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
독자 입력 요청
이 치트 시트에 포함되어야 한다고 생각되는 내용이 누락되어 있습니까? 아래 토론에서 공유하고 필요에 따라 이 게시물을 수정하겠습니다!
Reference
이 문제에 관하여(Go-To Rails 명령줄 치트 시트!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/fromwentzitcame/your-go-to-rails-command-line-cheat-sheet-1ok7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)