Rails第一課

1、配置數(shù)據(jù)庫(kù)

在config/database.yml中

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

還可以配置mysql

development:  
  adapter: mysql2
  encoding: utf8  
  host: 127.0.0.1  
  port: 3306  
  database: kfzs_web
  pool: 10  
  username: root
  password: superman
  socket: /tmp/mysql.sock

生成數(shù)據(jù)庫(kù)

rake db:create

3.創(chuàng)建新的頁(yè)面

rails generate controller home index

此時(shí)會(huì)創(chuàng)建好幾個(gè)文件:
app/view/home/index.html.erb

1、刪除文件
rm public/index.html
2、修改config/routes.rb文件
root :to => "home#index"

4.命令行創(chuàng)建model和生成表

創(chuàng)建一個(gè)Model叫Post有成員name,title數(shù)據(jù)類型為string,content類型為text

rails generate scaffold Post name:string title:string content:text

生成表

rake db:migrate

自動(dòng)生成字段id, created_at, updated_at,因?yàn)榕渲枚鄠€(gè)環(huán)境的數(shù)據(jù)庫(kù),可以在生成表的時(shí)候選擇數(shù)據(jù)庫(kù)

rake db:migrate RAILS_ENV=production

5.生成了那些文件

+models/post.rb
+controllers/posts_controller.rb
+views/posts目錄和目錄下生成的文件
* _form.html.erb
*  edit.html.erb
* index.html.erb
* new.html.erb
* show.html.erb
通過(guò)rake routes查看當(dāng)前的路由表

3.環(huán)境

if Rails.env == 'development'
if Rails.env.development?
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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