Sinatra 用 sinatra/mongoid/Carrierwave 如何实现文件上传

whatisruby · 2012年12月10日 · 3556 次阅读

自己瞎搞,出现个问题,麻烦大家看看 Model:

class User
  include Mongoid::Document
  field :avatar
  mount_uploader :avatar, AvatarUploader
end

页面:

<input name='avatra' type='file' />

处理:

post '/upload'  do
  user = User.find(params[:id])
#pramas[:avatra]返回的是
#{:filename=>"account_store.png", :type=>"image/png", :name=>"avatra", :tempfile=><File:/tmp/RackMultipart20121210-4172-ezb7jv>, :head=>"Content-Disposition: form-data; name=\"avatra\"; filename=\"account_store.png\"\r\nContent-Type: image/png\r\n"}

  user.avatra = pramas[:avatra]
  user.save  # 这里出错
end

错误信息:

NoMethodError - undefined method `__bson_dump__' for #<File:/tmp/RackMultipart20121210-4026-eu6nlb>:
    /home/ivan/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.6/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'
    /home/ivan/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.6/lib/moped/bson/extensions/hash.rb:35:in `each'
    /home/ivan/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.6/lib/moped/bson/extensions/hash.rb:35:in `__bson_dump__'
    /home/ivan/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.6/lib/moped/bson/extensions/hash.rb:36:in `block in __bson_dump__'

我在 gems/moped-1.1.6/lib/moped/bson/extensions/hash.rb:36 这行代码看了下, 我猜大概是跟 pramas[:avatra] 返回的是 Hash 有关

麻烦大家帮忙看看,我应该怎么做?谢谢

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号