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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With