Rails內建的資料庫是採用SQLite,資料庫的設定檔在此目錄下:config/database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
建立一個Model
$>rails g model users account:string password:string
invoke active_record
create db/migrate/20120520124738_create_users.rb
create app/models/users.rb
invoke test_unit
create test/unit/users_test.rb
create test/fixtures/users.yml
使用migrate建立資料表
$> bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0014s
== CreateUsers: migrated (0.0015s) ===========================================
新增一個descript欄位到users表格
rails g migration add_descript_to_users
invoke active_record
create db/migrate/20120520125033_add_descript_to_users.rb
執行migrate將剛新增的欄位加入
$>bundle exec rake db:migrate
== AddDescriptToUsers: migrating =============================================
== AddDescriptToUsers: migrated (0.0000s) ====================================
TIP:Rails會知道哪些已經執行過migration,所以只會執行一次
Rake指令集
rake db:create # to create database
rake db:migrate # to create tables based on your migration
星期日, 5月 20, 2012
星期六, 5月 19, 2012
[RoR] 第一個RoR應用程式: HelloWorld
來個所有程式的第一步吧,HelloWorld
測試環境:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0]
Rails 3.2.3
作業1:HelloWorld 起步
測試環境:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0]
Rails 3.2.3
作業1:HelloWorld 起步
星期二, 5月 08, 2012
星期二, 4月 24, 2012
星期日, 4月 22, 2012
[RoR] RoR 初探
最近開發接觸的語言,擷錄Ruby on Rails 實戰聖經心得 XD
Ruby on rails(ROR)
Rails的哲學包括以下指導原則:
- 不要重複自己(DRY: Don’t Repeat Yourself) – 撰寫出重複的程式碼是件壞事
- 慣例勝於設計(Convention Over Configuration) – Rails會預設各種好的設定跟慣例,而不是要求你設定每一個細節到設定檔中。
- REST是網站應用程式的最佳模式 – 使用Resources和標準的HTTP verbs(動詞)來組織你的應用程式是最快的方式(我們會在路徑一章詳細介紹這個強大的設計)
Rails是採用Ruby開發的網頁框架,David Heinemeier Hanson(DHH)在2004年發明,具有以下功能:
- MVC(Model-View-Control)模式
- 支援單元測試和整合測試
- 支援Ajax和RESTful介面
- ORM機制
- 支援各種最新的業界標準像是HTML5、JQuery
訂閱:
意見 (Atom)
