不确定这里发生了什么,但我认为我的嵌套表格部分导致了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目录中.它从未保存到数据库中,也没有移动到文件系统中.
有任何想法吗?
解决方法
一些可能的方案:
>它看起来像你有,但只是为了确保 – 你有家庭模型的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