Mongomapper和Sinatra undefined方法`update_attributes'代表nil:NilClass

时间:2012-05-05 22:08:28

标签: ruby sinatra mongomapper

在将Sinatra与MongoMapper一起使用时,我得到错误“未定义的方法`update_attributes'为nil:NilClass”

型号:

class Publication 
include MongoMapper::Document

key :page, String
key :title, String
key :body, String
end

代码我收到错误:

post '/edit' do 
@article = Publication.update({:page => 'main'}, {$set => {:title => params[:title], :body => params[:body]}})
end

我尝试在 mongo控制台中进行更新,并且已成功更新:

> db.publications.update({page: 'main'},{$set: {body: '333', title: '111'}})
> db.publications.find({page: 'main'})
{ "_id" : ObjectId("4fa45046530a6978e3000003"), "body" : "333", "page" : "main", "title" : "111" }

完整堆栈跟踪

[2012-05-06 03:04:22] INFO  WEBrick 1.3.1
[2012-05-06 03:04:22] INFO  ruby 1.8.7 (2010-01-10) [universal-darwin11.0]
== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from WEBrick
[2012-05-06 03:04:23] INFO  WEBrick::HTTPServer#start: pid=32266 port=4567
localhost - - [06/May/2012:03:04:27 MSK] "GET /edit HTTP/1.1" 200 193
- -> /edit
localhost - - [06/May/2012:03:04:27 MSK] "GET /favicon.ico HTTP/1.1" 404 447
- -> /favicon.ico
NoMethodError - undefined method `update_attributes' for nil:NilClass:
/Library/Ruby/Gems/1.8/gems/mongo_mapper-  0.11.1/lib/mongo_mapper/plugins/querying.rb:118:in `update_single'
/Library/Ruby/Gems/1.8/gems/mongo_mapper-0.11.1/lib/mongo_mapper/plugins/querying.rb:117:in `tap'
/Library/Ruby/Gems/1.8/gems/mongo_mapper-0.11.1/lib/mongo_mapper/plugins/querying.rb:117:in `update_single'
/Library/Ruby/Gems/1.8/gems/mongo_mapper-0.11.1/lib/mongo_mapper/plugins/querying.rb:45:in `update'
main.rb:48:in `POST /edit'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1211:in `call'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1211:in `compile!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `[]'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `route!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:801:in `route_eval'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `route!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:822:in `process_route'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:820:in `catch'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:820:in `process_route'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:784:in `route!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:783:in `each'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:783:in `route!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:886:in `dispatch!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `catch'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:705:in `call'
/Library/Ruby/Gems/1.8/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/Library/Ruby/Gems/1.8/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
/Library/Ruby/Gems/1.8/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
/Library/Ruby/Gems/1.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/Library/Ruby/Gems/1.8/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/showexceptions.rb:21:in `call'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1416:in `synchronize'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/handler/webrick.rb:13:in `run'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1295:in `run!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.2/lib/sinatra/main.rb:25
main.rb:53

任何人都可以说出于什么原因我没有在ruby代码中正确更新?

1 个答案:

答案 0 :(得分:3)

您可能希望将:$set代替$set作为您的密钥:

@article = Publication.update({:page => 'main'}, {:$set => {:title => params[:title], :body => params[:body]}})

您所拥有的是使用全局变量$set作为密钥,nil(因为所有全局变量都默认为nil)。例如:

{ $set => "something" }  #=> {nil=>"something"}
{ :$set => "something" } #=> {:$set=>"something"}