我正在使用Sinatra和Datamapper在Ruby中編寫一個簡單的應用程序,並且遇到了麻煩 - 當我嘗試將數據保存到SQLite數據庫時,沒有任何更改。但是,當我嘗試安裝數據庫或從irb更改數據時,它可以很好地工作。Datamapper不會將數據保存到數據庫中
,這裏是我的DataMapper的設置,模型和數據庫安裝方法(這工作正常):
DataMapper.setup(:default, "sqlite3://#{File.dirname(__FILE__)}/db.sqlite") class Page include DataMapper::Resource property :id, Serial property :parent_id, Integer property :title, String, :length => 0..255 property :slug, String, :length => 0..255 property :body, Text property :created_at, DateTime def children Page.all(:parent_id => self.id) end def install DataMapper.auto_migrate! Page.new(:parent_id => 0, :title => "Main", :slug => "/", :body => "This is the Main Page. Replace it's text with yours", :created_at => Time.now).save! end end
下面是一段代碼,無法正常工作:
post %r{/admin/edit/([\d]+)/?} do protected! #works fine and gets a row from database @page = Page.get(params[:captures].first) #update doesn't work, the save! method doesn't work too @page.update :title => params[:title], :parent_id => params[:parent_id], :slug => params[:slug], :body => params[:body] redirect request.path_info end
這工作正常在irb中:
p = Page.get(1) p.update :title => "testing update"
有誰知道有什麼問題?
P.S:我目前在Windows 7中工作,紅寶石版本是1.9.1p243(2009-07-16修訂24175)
哦,我忘了寫在這裏的解決方案。爲了讓這件事情起作用,我不得不將to_i方法應用於params [:parent_id]。 – 2010-03-12 07:35:38