2011-08-09 19 views
3

シンプルなhas_many多相関係を実装するには、GridFS with Mongoid and CarrierWaveをフォローしています。アバター、ネストされたアトリビュートの割り当てを通じて新しいユーザーを作成しようとすると、取得:ActionDispatch :: Http :: UploadedFileクラスのオブジェクトをBSONにシリアル化できません

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" 
+1

これを実行する場合は、フォームフィールドの名前が正しく指定されていることを確認してください。上記のコードは、入力フィールド名とアソシエーション名が一致していないと仮定しても問題ありません。与えられた例の – tgg

答えて

2

それはすべてのフィールドに名前を付けることです。

mongo_mapperとフィールドで同じ問題が発生しましたが、フォーム上で名前が変更されました。

フォームフィールドは、モデル内のフィールドの名前と同じ名前でなければなりません。

+0

は名前が一致しませんか? – dwaynemac

関連する問題