无法将 ActionDispatch::Http::UploadedFile 类的对象序列化为 BSON
我正在关注 GridFS 与 Mongoid 和CarrierWave 实现一个简单的 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"
I'm following GridFS with Mongoid and CarrierWave to implement a simple has_many polymorphic relationship and when I attempt to create a new user, with an avatar, through nested attribute assignment, I get:
Cannot serialize an object of class ActionDispatch::Http::UploadedFile into BSON
Has anyone else encountered this? I noticed a few individuals posted replies to the "GridFS with Mongoid and CarrierWave" article but I was unable to find anyone with an answer.
# 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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这都是关于命名字段的。
mongo_mapper 和我在表单上重命名的字段也有同样的问题。
表单字段必须与模型中的字段具有相同的名称。
It's all about naming the fields.
Had the same issue with mongo_mapper and a field I have renamed on the form.
The form fields have to have the same names of the fields in you model.