環(huán)境配置
- 工具配置
// 下載python3
brew search python3
brew install python3
// 安裝python3的第三方包管理工具pip3
// https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip 文件另存為.py
python3 get-pip.py
// pip3安裝第三方包
pip3 install -r requirements.txt
// pip3 freeze > requirements.txt
// 下載gpg(文件加密)
brew install gnupg gnupg2
curl -sSL https://rvm.io/mpapis.asc | gpg --import -
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
// 安裝rvm (rvm是ruby的一個(gè)版本環(huán)境控制器)
\curl -sSL https://get.rvm.io | bash -s stable --ruby
// 安裝ruby2.3
rvm install 2.3
// 切換至 ruby2.3
rvm use 2.3
// 安裝 包管理
gem install bundler
cd /backend/trunk/
bundle install
- rails路由配置 (routes.rb)
get 'welcome/first'
get 'welcome/second'
# 根目錄(welcome 控制器下的 first 活動(dòng))
root 'welcome#first'
# resource 方法(會(huì)自動(dòng)生成welcome資源下所有的訪問(wèn))
resource welcome
rails 命令
# 創(chuàng)建controller
rails generate controller welcome
# 刪除一個(gè)controller
rails destroy controller welcome
# 創(chuàng)建controller同時(shí)創(chuàng)建first 和 second活動(dòng)
rails generate controller welcome first second
# 查看工程路由信息
rake routes
# 創(chuàng)建model的migrate
rails generate model User name:string descrip:text time:datetime
# 根據(jù)migrate 創(chuàng)建表
rake db:migrate
# 創(chuàng)建文件
touch app/views/user/new.html.erb
實(shí)例如下:
// 查看rails new 命令的參數(shù)
rails help new
// 創(chuàng)建一個(gè)名字為blog的rails項(xiàng)目
rails new blog --skip-bundle
// 通過(guò)bundle安裝依賴的gem文件
bundle install
// rails generate 創(chuàng)建一個(gè)post模型:后面是他的字段
rails generate model Post title:string content:text author:string publish_at:datetime
// 下面是上述命令執(zhí)行的結(jié)果,創(chuàng)建了幾個(gè)文件
invoke active_record
create db/migrate/20180310080403_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
// rake命令通過(guò)生成的(db目錄下的)migrate來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行創(chuàng)建
rake db:migrate
// 下面是rake命令執(zhí)行結(jié)果
== 20180310080403 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0016s
== 20180310080403 CreatePosts: migrated (0.0017s) =============================
// rails c進(jìn)入到命令行查看是否創(chuàng)建成功Post類
rails c
Loading development environment (Rails 4.2.7.1)
2.3.4 :001 > Post
// 下面是結(jié)果瞒滴,顯示有這個(gè)類(子段名以及類型)
=> Post(id: integer, title: string, content: text, author: string, publish_at: datetime, created_at: datetime, updated_at: datetime)
// 創(chuàng)建一個(gè)包含index動(dòng)作的Welcome控制器
rails generate controller Welcome index
// 下面是上述代碼執(zhí)行結(jié)果
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss
使用 rails 腳手架工具生成完整資源套件(模版啥的):
# 使用腳手架工具生成所有需要的配置信息(controller model view 等)
rails generate scaffold Post content:text
牢牢記住各個(gè)action動(dòng)作默認(rèn)做的事情:
# 獲取模型列表
def index
@posts = Post.all
end
# 獲取詳情(根據(jù)params id字段查詢并且用show.html.erb顯示)
def show
# 此方法實(shí)際上是下面代碼 隱式實(shí)現(xiàn)的
# @posts = Post.find params[:id]
end
往已存在的rails model增加字段
往已存在的User中增加password字段
- 正規(guī)方法:
# 1. 創(chuàng)建一個(gè)migration 規(guī)范名字(直接生成一個(gè)寫(xiě)好的migration文件)
rails generate migration add_user_id_to_posts user_id:id
# 自動(dòng)創(chuàng)建好的文件
class AddNikeNameToUsers < ActiveRecord::Migration
def change
add_column :users, :nike_name, :string
end
end
# 2. rake一下migration文件
Mac-mini-2:learnproject jing$ rake db:migrate
# 1. 創(chuàng)建一個(gè)migration 名字隨便起
rails g migration AddColumnPasswordToUser
# 結(jié)果:create db/migrate/20180314055455_add_column_password_to_user.rb
# 2. 編輯migration 文件(按照需求編輯migration文件)
class AddColumnPasswordToUser < ActiveRecord::Migration
def change
add_column :users,:password,:string
end
end
# 3. rake一下migration文件
rake db:migrate
- 簡(jiǎn)單方法:(不正規(guī))
# 1. 直接修改 create_user.rb migrate文件
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.timestamps null: false
end
end
end
# 2. 然后去數(shù)據(jù)庫(kù)中添加一列 password砰识,不需要rake migrate文件
關(guān)聯(lián)數(shù)據(jù)
users表 和posts表 關(guān)聯(lián)
# 1. 新加一個(gè)id字段
rails generate migration add_user_id_to_posts user_id:id
rake db:migrate
# 2. rails model關(guān)聯(lián)(belongs_to 和 has_many )
# 一對(duì)多
class Post < ActiveRecord::Base
belongs_to :user
end
# 多對(duì)一
class User < ActiveRecord::Base
has_many :posts
end
# 3. post save方法時(shí)候
@post.user_id = session[:user_id]
@post.save
# 然后就可以這樣獲取user
post.user.userName
# 多對(duì)多關(guān)系建立
# 1. has_and_belongs_to_many 關(guān)鍵字
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
# 2. create_join_table: categories,:posts 建立一個(gè)關(guān)聯(lián)表
rails g migration create_join_table_for_posts_and_categories
# 編輯
class CreateJoinTableForPostsAndCategories < ActiveRecord::Migration
def change
create_join_table :categories,:posts
end
end
# 3. 更新
rake db:migrate
單元測(cè)試
# 運(yùn)行測(cè)試文件命令
bundle exec rake test
# test如下
class UserControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end
model相關(guān)
# 手動(dòng)添加數(shù)據(jù)到數(shù)據(jù)庫(kù)
Mac-mini-2:my_custom_blog jing$ rails c
Loading development environment (Rails 4.2.7.1)
2.3.4 :001 > Post.create(title:'first post',content:'content of first post',author:'jing',published_at:Time.now)
# 序列化model的屬性
class Post < ActiveRecord::Base
serialize :tags
end
# 手動(dòng)修改數(shù)據(jù)庫(kù)記錄
p = Post.first
p.tags = ['blog','post']
p.save