无法将 ActionDispatch::Http::UploadedFile 类的对象序列化为 BSON

发布于 2024-11-28 18:30:10 字数 1696 浏览 1 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-12-05 18:30:10

这都是关于命名字段的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文