gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:26:22 24 4
gpt4 key购买 nike

我正在关注GridFS with Mongoid and 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"

最佳答案

这都是关于命名字段的。

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

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

关于mongoid - 无法将 ActionDispatch::Http::UploadedFile 类的对象序列化为 BSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7003154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com