2011-02-15 106 views
8

不知道这里发生了什么,但我认为我的嵌套窗体部分导致了CarrierWave的问题。Carrierwave上传嵌套窗体?

当我用上传的文件更新一个字段时,没有任何反应:没有错误,但没有任何存储。

我有一个与“个人”模型具有“has_many”关系的“家庭”模型。在 “个人” 模式有一个 “图片报” 载:

class Individual < ActiveRecord::Base 
    belongs_to :household 
    mount_uploader :picture, PictureUploader 
end 

在我的观点,我有:

= form_for @household, :html => {:multipart => true} do |f| 

,然后调用部分为个人:

= f.fields_for :individuals do |builder| 
    = render 'individual_fields', :f => builder 

= f.submit 

局部只是有以下几种:

= f.label :firstname, 'First' 
= f.text_field :firstname, :size => 10 
= f.label :lastname, 'Last' 
= f.text_field :lastname, :size => 15 
= f.file_field :picture 

上传的图片出现在参数中:

Started POST "/households/849" for 127.0.0.1 at 2011-02-15 15:45:16 -0500 
    Processing by HouseholdsController#update as HTML 
    Parameters: {"...6/1/2008; Active 6/6", "individuals_attributes"=>{"0"=>{"firstname"=>"Hannah", ... 
    "picture"=>#<ActionDispatch::Http::UploadedFile:0xb9fbd24 @original_filename="3.jpg", 
    @content_type="image/jpeg", @headers="Content-Disposition: form-data; 
    name=\"household[individuals_attributes][1][picture]\"; filename=\"3.jpg\"\r\nContent-Type: 
    image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20110215-6498-ba4bp>>, "_destroy"=>"false", 
    "id"=>"4077"}}}, "commit"=>"Update Household", "id"=>"849"} 

并存储在上传路径下的tmp目录下。它从来没有保存到数据库中,也没有移到文件系统中。

任何想法?

回答

7

一些可能的解决方案:

  • 看起来你有,但只是为了确保 - 你有对家庭模式accepts_nested_attributes?
  • 另外,你有没有尝试过,而不是部分本地化的问题?
  • 你在PictureUploader模型上有Rmagick或minimagick吗?

此外,您还需要注意Carrierwave和嵌套窗体的已知问题,详见Carrierwave Wiki

解决方法是添加下面的方法:

class Image < ActiveRecord::Base 
    mount_uploader :image, ImageUploader 

    def image=(val) 
    if !val.is_a?(String) && valid? 
     image_will_change! 
     super 
    end 
    end 

end 
class Person < ActiveRecord::Base 
    has_many :images 
    accepts_nested_attributes_for :images 
end 
+0

啊, “DEF =图像=(VAL)” 的东西为我工作。我曾看到过这个入口,但不管它是否适用于我,无论出于何种原因。只是一个说明:它应该是“**接受** _nested_attributes_for”。谢谢! – thermans 2011-02-17 21:39:17