2012-02-29 2 views
3

임베디드 클래스의 컨트롤러에 무엇이 있어야하는지 잘 모르겠습니다. 전달 된 매개 변수는 이미지 속성을 포함하여 모든 내장 클래스 속성을 표시하지만 비 이미지 매개 변수 만 데이터베이스에 저장됩니다.레일 : 반송파가있는 중첩 된 폼

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 
,

부모 양식 :

<%= form_for(@article, :html => {:multipart => true}) do |f| %> 

    <div class = "field"> 
     <%= f.label :name %><br /> 
     <%= f.text_field :name %> 
    </div> 

    <%= f.fields_for :comments do |c| %> 
    <p> 
     <%= c.label :name %> 
     <%= c.text_field :name %> 
    </p> 

    <p> 
     <%= c.label :image, "Select Screenshot from your Computer"%><br /> 
     <%= c.file_field :image %> 
    </p> 

    <p> 
     <%= c.label :remote_image_url, "or URL from the interweb"%><br /> 
     <%= c.text_field :remote_image_url %> 
    </p> 

    <% end %> 


    <div class = "actions"> 
     <%= f.submit %> 
    </div> 
<% end %> 
+0

나는 이전에 같은 문제에 직면 해있다. Dave는 나를 위해 일한다. 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

고마워요! 이것은 문제를 해결했습니다. –