如果是windows下的用戶,而又不是使用instant Rail,那么需要進行以下步驟。
1、下載sqlite的exe和dll文件,然后將其放入系統(tǒng)path。(有些linux發(fā)行版本默認安裝了sqlite,無需再次安裝)
2、確定你下載的sqlite版本,如果是sqlite3(注意放入path目錄的文件應該保持的sqlite3.exe和sqlite3.dll,不要改名為sqlite.exe和sqlite.dll),在命令行運行
gem install sqlite3-ruby
安裝sqlite3的ruby驅動。
3、新建一個Rails程序
本想自己寫點代碼,可是網(wǎng)上有個5行的todo,我就懶了。
rails todo
這時使用的是默認的sqlite3做數(shù)據(jù)庫。如果你希望使用mysql,則輸入
rails todo -d mysql
有點rails經(jīng)驗的人會發(fā)現(xiàn)這個“-d”的新東西。如果你是在mysql下,往往需要修改config目錄下的database.yml文件。
development:adapter: mysqlencoding: utf8database: blog_developmentusername: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.socktest:adapter: mysqlencoding: utf8database: blog_testusername: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.sockproduction:adapter: mysqlencoding: utf8database: blog_productionusername: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.sock不過有些人覺得這樣很不爽,于是有了這樣的
defaults: &defaultsadapter: mysqlencoding: utf8username: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.sockdevelopment:database: blog_development<<: *defaultstest:database: blog_test<<: *defaultsproduction:database: blog_production<<: *defaults
當然出于安全考慮,誰也不會用這樣的配置去搞到生產(chǎn)環(huán)境下。不過這樣看著確實爽多了。
2、新建數(shù)據(jù)庫
既然上面配置好了,那么下面就該實際的聯(lián)起來用了。
cd todorake db:create:all
這里又一個新東西“rake db:create:all”,它將給你建立起各個數(shù)據(jù)庫,現(xiàn)在不需要你自己去手工搞了。是不是比以前爽了。
D:\work\todo>rake db:create:all(in D:/work/todo)"db/development.sqlite3 already exists""db/production.sqlite3 already exists""db/test.sqlite3 already exists"上面是我這里運行成功的提示。
下面是個說明
db:charset Retrieves the charset for the current environment’s databasedb:collation Retrieves the collation for the current environment’s databasedb:create Create the database defined in config/database.yml for the current RAILS_ENVdb:create:all Create all the local databases defined in config/database.ymldb:drop Drops the database for the current RAILS_ENVdb:drop:all Drops all the local databases defined in config/database.ymldb:reset Drops and recreates the database from db/schema.rb for the current environment.db:rollback Rolls the schema back to the previous version. Specify the number of steps with STEP=ndb:version Retrieves the current schema version number
這里注意有了個新的“db:rollback”命令,比以前用爽多了。
rake db:migrate VERSION=xxx可以說byebye了。
3、真正的算代碼的東西就一行
ruby script/generate scaffold Todo title:string body:text done:boolean due:datetime
前幾個月大家還在感嘆model里面竟然可以那樣sexyness,現(xiàn)在看看這個直接在命令行搞定,現(xiàn)在該用啥詞形容好呢。
最后別忘記
rake db:migrate
4、運行起來看看。
ruby script/server
然后用瀏覽器訪問下面的鏈接127.0.0.1:3000/todos
搞定了一個todolist。