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?