2012-02-29 8 views
3

組み込みクラスのコントローラにはどうすればよいか分かりません。渡されたパラメータには、画像属性を含むすべての埋め込みクラス属性が表示されますが、非画像パラメータのみがデータベースに保存されます。Rails:搬送波を含むネストされたフォーム

Parameters: {"article"=>{"name"=>"New article", "comments_attributes"=>{"0"=>{"remote_image_url"=>"", "name"=>"Comment 1", "content"=>"comment content....", "image"=>#<ActionDispatch::Http::UploadedFile:0x10339d880 @headers="Content-Disposition: form-data; name=\"article[comments_attributes][0][image]\"; filename=\"dh.png\"\r\nContent-Type: image/png\r\n", @original_filename="dh.png", @tempfile=#<File:/var/folders/A1/A1SUPUTUFA8BYB5j+RD2L++++TI/-Tmp-/RackMultipart20120228-21178-1vckii1-0>, @content_type="image/png">}}, "content"=>"article content"}, "commit"=>"Create Article", "authenticity_token"=>"i14YuJs4EVKr5PSEw9IwKXcTbQfOP4mjbR95C75J2mc=", "utf8"=>"\342\234\223"} 
MONGODB (89ms) freedb['system.namespaces'].find({}) 
MONGODB (0ms) freedb['articles'].insert([{"name"=>"New article", "comments"=>[{"name"=>"Comment 1", "_id"=>BSON::ObjectId('4f4daf6a58001652ba000012'), "content"=>"comment content...."}], "_id"=>BSON::ObjectId('4f4daf6958001652ba000011'), "content"=>"article content"}]) 

親モデル::

class Article 
    include Mongoid::Document 
    field :name, :type => String 
    field :content, :type => String 

    embeds_many :comments 

    accepts_nested_attributes_for :comments 
end 

子モデルこれは、問題は(この場合はMongoid)ORMの選択肢ではありませんが、私は、搬送波を使用している方法に関連していることを私に伝えます:

require 'carrierwave/mongoid' 

class Comment 
    include Mongoid::Document 
    field :name, :type => String 
    field :content, :type => String 

    field :image, :required => true 
    field :remote_image_url 

    embedded_in :article, :inverse_of => :comments 
    mount_uploader :image, ImageUploader 
end 

親コントローラ:

def new 
    @article = Article.new 
    @article.comments.build 
    end 

    def create 
    @article = Article.new(params[:article]) 
    end 

親フォーム:

​​
+0

私は前に同じ問題に直面しています。デイブは私のために働いています。 http://stackoverflow.com/questions/6447278/uploading-multiple-files-at-once-to-rails-app-with-carrierwave-html5 – Tim

答えて

8

Carrierwaveは、モデル内の画像やデータを保存するために、いくつかのコールバックを使用します。デフォルトでは、組み込みモデルはコールバックを実行しません。埋め込みコードでコールバックを実行する必要があることを明示的に述べる必要があります。

これを実行するには、cascade_callbacks: trueオプションを使用します。

embeds_many :comments, cascade_callbacks: true 
+0

ありがとう、これは完全に助けました。 – noazark

+0

ありがとう!これは問題を解決しました。 –

関連する問題