我正在关注GridFS with Mongoid and CarrierWave以实现一个简单的has_many多态关系,当我尝试通过嵌套属性赋值创建一个具有头像的新用户时,我得到:
Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON
还有其他人遇到过这个吗?我注意到有几个人发表了对“带有Mongoid和CarrierWave的GridFS”文章的回复,但是我找不到任何有答案的人。
# 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"
答案 0 :(得分:2)
这就是命名字段。
mongo_mapper和我在表单上重命名的字段存在同样的问题。
表单字段必须与您模型中的字段具有相同的名称。