天天看點

Ruby on Rails 實踐

[color=indigo][b]***** Ruby on Rails 實踐 讀書筆記一 *****[/b][/color]

ruby script\generate model book

ruby script\generate controller book

[b]Rails連接配接Sql Server問題[/b]

從[url=http://rc.org.cn/group_id=234&release_id=25125.html]RubyForge[/url]下載下傳[url=http://www.rayfile.com/files/a5c00902-96af-11dd-ae89-0014221b798a/]dbi-0.2.2.tar.gz[/url] [img]http://static.rayfile.com/media/img/icon_download.gif[/img],\lib\dbd\ADO.rb複制到Ruby安裝目錄\lib\ruby\site_ruby\1.8\DBD\ADO 下,更改config\database.yml。

# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter: sqlserver
  database: mybook
  host: localhost
  username: sa
  password: dol

# 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: sqlserver
  database: mybook
  host: localhost
  username: sa
  password: dol

production:
  adapter: sqlserver
  database: mybook
  host: localhost
  username: sa
  password: dol
           

[b]新版本Rails的scaffold問題[/b]

ruby script/plugin install scaffolding

ruby script/plugin install http://tools.assembla.com/svn/breakout/breakout/vendor/plugins/classic_pagination/

[color=red]暫時不明白這個指令的含義

ruby script\generate scaffold book title:string description:text buydate:datetime[/color]

http://127.0.0.1:3000/book/new能夠打開,但點選Create報錯

Unknown action

No action responded to /book/created

[color=indigo][b]***** Rails安裝問題 *****[/b][/color]

使用rubygems安裝rails,執行遠端指令[color=red]gem install rails[/color],但不知為何始終無法成功,可能是網絡問題,報錯資訊如下:

[quote]ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)

SocketError reading http://gems.rubyforge.org/gems/activerecord-2.1.1.gem[/quote]

于是決定手動安裝,在[url=http://rc.org.cn/group_id=307&release_id=25692.html]RubyForge[/url]下載下傳[url=http://www.rayfile.com/files/c8ebc6f5-980c-11dd-b7d1-0014221b798a/]rails-2.1.0.zip[/url] [img]http://static.rayfile.com/media/img/icon_download.gif[/img] 和[url=http://www.rayfile.com/files/c08d9882-980b-11dd-81c6-0014221b798a/]rails-2.1.0.gem[/url] [img]http://static.rayfile.com/media/img/icon_download.gif[/img],存于F:\Ruby\source目錄下,在此路徑下執行[color=red]gem install rails rails-2.1.0.gem[/color],依舊同樣錯誤。

執行[color=red]gem update --system[/color]更新gem版本後,執行[color=red]gem -v[/color]檢視版本已從0.9.4升至1.3.0。

再次安裝:

[quote]F:\Ruby\source>[color=red]gem install rails rails-2.1.0.gem[/color]

Successfully installed activeresource-2.1.1

Successfully installed rails-2.1.1

ERROR: could not find gem rails-2.1.0.gem locally or in a repository

2 gems installed

Installing ri documentation for activeresource-2.1.1...

Installing RDoc documentation for activeresource-2.1.1...

F:\Ruby\source>[color=red]gem -v[/color]

1.3.0

F:\Ruby\source>[color=red]gem install rails[/color]

Successfully installed rails-2.1.1

1 gem installed

F:\Ruby\source>[color=red]rails -v[/color]

Rails 2.1.1[/quote]

[color=indigo][b]***** Ruby on Rails 實踐 讀書筆記二 *****[/b][/color]

反複試了幾個小時,終于在我的Sql Server 2000環境中完成實踐裡的CRUD功能。

1、下載下傳[url=http://www.rayfile.com/files/1cf8dd05-982b-11dd-acc2-0014221b798a/]sqlserver_adapter.rb[/url] [img]http://static.rayfile.com/media/img/icon_download.gif[/img] 存于{Ruby安裝目錄}\lib\ruby\gems\1.8\gems\activerecord-2.1.1\lib\active_record\connection_adapters下。

2、下載下傳[url=http://www.rayfile.com/files/a5c00902-96af-11dd-ae89-0014221b798a/]dbi-0.2.2.tar.gz[/url] [img]http://static.rayfile.com/media/img/icon_download.gif[/img],解壓後将\lib\dbd\ADO.rb複制到{Ruby安裝目錄}\lib\ruby\site_ruby\1.8\DBD\ADO下。

3、執行F:\Ruby\railsdoc>[color=red]rails booksample2[/color](railsdoc是我的項目目錄)建立工程檔案夾,rails将生成該應用程式的完整目錄結構。

4、修改booksample2\config\database.yml檔案,如下:

adapter: sqlserver
database: mybook
host: localhost
username: sa
password: dol
           

5、執行F:\Ruby\railsdoc\booksample2>[color=red]ruby script/generate scaffold Book title:string description:text buydate:date[/color],為Book類建立更多相關的資料庫移植檔案及model、controller、views檔案。

6、先後執行[color=red]rake db:create[/color]建立資料庫和[color=red]rake db:migrate[/color]建立表結構,但發生了以下錯誤:

[quote]F:\Ruby\railsdoc\booksample2>[color=red]rake db:create[/color]

(in F:/Ruby/railsdoc/booksample2)

F:\Ruby\railsdoc\booksample2>[color=red]rake db:migrate[/color]

(in F:/Ruby/railsdoc/booksample2)

rake aborted!

Open

OLE error code:80004005 in Microsoft OLE DB Provider for SQL Server

無法打開登入 'mybook' 中請求的資料庫。登入失敗。

HRESULT error code:0x80020009

發生意外。

(See full trace by running task with --trace)[/quote]

打開資料庫發現資料庫并未建立[b][i][color=green](Why?...)[/color][/i][/b],于是在企業管理器中手動建立了一個名為mybook的資料庫,再執行[color=red]rake db:migrate[/color],如下(如不執行此指令而手動建表的話,Create時會顯示主鍵字段無法插入NULL值的錯誤資訊):

[quote]F:\Ruby\railsdoc\booksample2>[color=red]rake db:migrate[/color]

(in F:/Ruby/railsdoc/booksample2)

== 20081012063300 CreateBooks: migrating ======================================

-- create_table(:books)

-> 0.0150s

== 20081012063300 CreateBooks: migrated (0.0310s) =============================[/quote]

7、執行[color=red]ruby script/server[/color]以啟動WEBrick伺服器,如下:

[quote]F:\Ruby\railsdoc\booksample2>[color=red]ruby script/server[/color]

=> Booting WEBrick...

=> Rails 2.1.1 application started on http://0.0.0.0:3000

=> Ctrl-C to shutdown server; call with --help for options

[2008-10-12 14:40:25] INFO WEBrick 1.3.1

[2008-10-12 14:40:25] INFO ruby 1.8.6 (2007-09-24) [i386-mswin32]

[2008-10-12 14:40:25] INFO WEBrick::HTTPServer#start: pid=3948 port=3000 [/quote]

8、在浏覽器鍵入http://127.0.0.1:3000/books打開頁面,但卻發現輸入中文是亂碼,且Create後報錯,如下:

[quote]DBI::DatabaseError: Execute OLE error code:80040E10 in Microsoft OLE DB Provider for SQL Server

至少一個參數沒有被指定值。[/quote]

9、在booksample2\config\environment.rb檔案中添加如下代碼:

[quote]require 'win32ole'

WIN32OLE.codepage = WIN32OLE::CP_UTF8 [/quote]

10、Ruby on Rails執行個體中的CRUD功能就完成了,如下圖所示:

[img]/upload/attachment/43446/d867a9a0-7501-34a2-9a28-0e9f76132c16.jpg[/img]

[quote][color=blue]F:\Ruby\railsdoc\booksample2>ruby -v

[b]ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32][/b]

F:\Ruby\railsdoc\booksample2>rails -v

[b]Rails 2.1.1[/b]

F:\Ruby\railsdoc\booksample2>gem -v

[b]1.3.0[/b][/color][/quote]

原空間位址:[url]http://rc.org.cn/9571/spacelist-blog.html[/url]

繼續閱讀