3

我正在使用带有 Mongoid 和 CarrierWave 的 GridFS来实现一个简单的 has_many 多态关系,当我尝试通过嵌套属性分配创建一个带有头像的新用户时,我得到:

Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON

有人遇到过这种情况么?我注意到一些人发布了对“GridFS with Mongoid and CarrierWave”文章的回复,但我无法找到任何有答案的人。

# app/models/asset.rb
class Asset
    include Mongoid::Document
    include Mongoid::Timestamps

    mount_uploader :file, AssetUploader

    field :name, type: String

    referenced_in :attachable, polymorphic: true
end
# app/models/user.rb
class User
    include Mongoid::Document
    include Mongoid::Timestamps

    references_one :avatar, as: :attachable

    accepts_nested_attributes :avatar
end
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
    config.grid_fs_connection = Mongoid.database
    config.storage = :grid_fs
    config.grid_fs_access_url = "/images"
end
# app/uploaders/asset_uploader.rb
class AssetUploader < CarrierWave::Uploader::Base  
end
# app/views/users/new.html.haml
= semantic_form_for(@user, html: { multipart: true }) do |f|
    = f.inputs do
    = f.semantic_fields_for :avatar do |af|
        = af.input :file, as: :file
    = f.buttons do
        = f.commit_button "Upload"
4

1 回答 1

2

这都是关于命名字段的。

mongo_mapper 和我在表单上重命名的字段有同样的问题。

表单字段必须与模型中的字段名称相同。

于 2011-10-01T21:07:55.477 回答