Rails 常用指令
Rails 的开发环境的搭建参考入手新 Mac 后的环境搭建
Rails 的关键知识学习请前往 Xdite 的全栈营
PS:
人们使用 Ruby 这门语言的原因仅仅是因为要用 Rails ,离开了 Rails , Ruby 这门语言就……所以你知道为啥说到 Ruby 就一定要说 Rails ,或者人们在谈论 Ruby 时,其实说的是 Rails 了吧
基本指令
## 在当前目录下新建 Rails app
$ rails new [app名]
## Example
$ rails new first_app
## 打开服务器(网址通常是 http://localhost:3000)
$ cd first_app
$ rails s
$ rails server
## 查看路径
$ rails routes
## 数据库管理 Migration
$ rails db:migrate ## runs (single) migrations that have not run yet. - update schema.rb with all migrations
$ rails db:create ## creates the database
$ rails db:drop ## deletes the database
$ rails db:schema:load ## creates tables and columns within the (existing) database following schema.rb
$ rails db:seed ## load seed.rb - 按照 seed.rb 文件里的指令,创造数据并添加到数据库里
$ rails db:setup ## does db:create, db:schema:load, db:seed
$ rails db:reset ## does db:drop, db:setup
$ rails db:rollback ## 取消最近一次 migration 的加载 - 通常不用,改数据库强烈建议新增 Migration 来改,而不是执行 rollback ,rollback 建议最多只运行一次(退后一步)
## 先 drop(删除)掉原先的数据库,再建立新的数据库,最后 migrate 加载所有 migrations
$ rails db:drop db:create db:migrate
## Example
$ rails g controller groups
$ rails g controller account/groups
$ rails g controller account::groups
Model
可以直接用一行代码新建 Model,适合 column 较少的情况
## 新建 Model
$ rails g model [model 的名字] [参数名:参数类型] [参数名:参数类型] ...
## Example
$ rails g model job title:string description:text wage_upper_bound:decimal wage_lower_bound:decimal contact:text is_hidden:boolean也可以先生成 migration 文件,然后编辑该文件来生成
以下命令会生成一个 migration 文件 - db/migrate/20180423162706_create_[model 的名字].rb
$ rails g model [model 的名字]
# Example
$ rails g model case
Running via Spring preloader in process 44069
invoke active_record
create db/migrate/20180423162706_create_cases.rb
create app/models/case.rb
invoke test_unit
create test/models/case_test.rb
create test/fixtures/cases.yml找到并打开编辑 db/migrate/20180423162706_create_[model 的名字].rb 文件
class CreateCases < ActiveRecord::Migration[5.1]
def change
create_table :cases do |t|
t.string :nickname
t.text :summary
t.timestamps
end
end
end无论采用上述哪种方式,最终都需要执行 rails db:migrate 来完成 Model 的建立
下面是如何给已有的 Model 添加 column
$ rails g migration [要添加的 migration 的名字]
## Example
$ rails g migration add_user_id_to_jobclass AddUserIdToJob < ActiveRecord::Migration[5.0]
def change
add_column :jobs, :user_id, :integer
end
end重命名 Column - 注意 job 要复数(之前新建 model 时 Job 用的是单数 - rails g model job title:string)
class ChangeWagesColumnNameInJob < ActiveRecord::Migration[5.1]
def change
rename_column :jobs, :upper_wage, :wage_upper_bound
rename_column :jobs, :lower_wage, :wage_lower_bound
end
endCRUD 模板
class GroupsController < ApplicationController
def index
@groups = Group.all
end
def new
@group = Group.new
end
def show
@group = Group.find(params[:id])
end
def edit
@group = Group.find(params[:id])
end
def create
@group = Group.new(group_params)
if @group.save
redirect_to groups_path
else
render :new
end
end
def update
@group = Group.find(params[:id])
if @group.update(group_params)
redirect_to groups_path, notice: 'Update Success'
else
render :edit
end
end
def destroy
@group = Group.find(params[:id])
@group.destroy
redirect_to groups_path, alert: 'Group deleted'
end
private
def group_params
params.require(:group).permit(:title, :description)
end
end新手常用 Gem
$ rails g devise:install
$ rails g devise user
$ rake db:migrate$ rails generate simple_form:install --bootstrap打赏