玩具應(yīng)用toy_app

創(chuàng)建toy-app應(yīng)用:

cd ~/workspace
rails new toy_app
cd toy_app/

參照使用Heroku部署hello_app,修改Gemfile文件,然后執(zhí)行:

bundle install --without production  #安裝gem
git init      #把toy_app納入git中
git add -A
git commit -m "Initialize repository"
git remote add origin git@bitbucket.org:<username>/toy_app.git
git push -u origin --all

參照創(chuàng)建第一個(gè)應(yīng)用hello_app修改toy_app應(yīng)用中的app/controllers/application_controller.rb文件,定義動(dòng)作。
提交改動(dòng),再推送到 Heroku 中:

git commit -am "Add hello"
heroku create
git push heroku master

Users資源:創(chuàng)建用戶
接下來(lái),利用腳手架scaffold,生成Users資源:

rails generate scaffold User name:string email:string
rails db:migrate    #遷移數(shù)據(jù)庫(kù)

執(zhí)行rails s命令后,在瀏覽器打開(kāi) http://localhost:3000/users 就可以看到,是一個(gè)可以創(chuàng)建用戶的界面了(好可惜,這里沒(méi)有截圖),并且可以創(chuàng)建、編輯、刪除用戶。

修改toy_app/config/routes.rb文件,改變跟路由:

Rails.application.routes.draw do
  resources :users
  root 'users#index'
end

Microposts資源:創(chuàng)建微博
同樣利用scaffold生成Microposts資源:

rails generate scaffold Micropost content:text user_id:integer
rails db:migrate

執(zhí)行rails s命令后,在瀏覽器打開(kāi) http://localhost:3000/microposts/new 就可以看到,是一個(gè)可以創(chuàng)建微博的界面了。試著輸入一些內(nèi)容吧。

在toy_app/app/models/micropost.rb中可以限制微博的長(zhǎng)度:

class Micropost < ApplicationRecord
  validates :content, length: { maximum: 140 }
end

修改toy_app/app/models/user.rb文件,設(shè)置一個(gè)用戶可擁有多篇微博:

class User < ApplicationRecord
  has_many :microposts
end

修改toy_app/app/models/micropost.rb,設(shè)置一篇微博屬于一個(gè)用戶:

class Micropost < ApplicationRecord
  belongs_to :user
  validates :content, length: { maximum: 140 }
end

修改toy_app/app/models/micropost.rb文件,添加驗(yàn)證微博內(nèi)容存在性的代碼:

class Micropost < ApplicationRecord
  belongs_to :user
  validates :content, length: { maximum: 140 },
                      presence: true
end

修改toy_app/app/models/user.rb,添加驗(yàn)證用戶名和郵件存在性的代碼:

class User < ApplicationRecord
  has_many :microposts
  validates :name, presence: true 
  validates :email, presence: true
end

部署:

git add -A
git commit -m "finished"
git push
heroku create 
git push heroku
heroku run rails db:migrate   #遷移生產(chǎn)數(shù)據(jù)庫(kù)

好了,今天的學(xué)習(xí)終于是在“碰到問(wèn)題to解決問(wèn)題”的過(guò)程中完成了,實(shí)踐出真理,動(dòng)動(dòng)腦筋,靈活學(xué)習(xí),任何困難都會(huì)迎刃而解的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,836評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評(píng)論 19 139
  • 剛剛在回顧運(yùn)營(yíng)了兩年的茉莉家族,365*2,365*2*24,17,520分鐘,每一分每一秒都與最值得的人在一起。...
    茉莉大大閱讀 366評(píng)論 0 0
  • 起床時(shí)間:5:30 運(yùn)動(dòng)項(xiàng)目:慢跑 成果:4.3 km,34分鐘,302 大卡
    溫暖的小胖紙閱讀 586評(píng)論 0 50
  • 一場(chǎng)盛大的宴會(huì),就這樣,落下了帷幕,鮮紅的帷幕一旦被拉上,我們的笑聲,淚水,就真的,再也不見(jiàn)。杯盤(pán)狼藉,曲終人散。...
    釵離閱讀 266評(píng)論 0 0

友情鏈接更多精彩內(nèi)容