带有嵌套表单的 Carrierwave 上传? [英] Carrierwave upload with nested forms?

查看:29
本文介绍了带有嵌套表单的 Carrierwave 上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定这里发生了什么,但我认为我的嵌套表单部分给 CarrierWave 造成了问题.

Not sure what's going on here, but I think my nested form partials are causing a problem for CarrierWave.

当我使用上传的文件更新字段时,没有任何反应:没有错误,但也没有存储任何内容.

When I update a field with an uploaded file, nothing happens: no error, but nothing stored either.

我有一个Household"模型,它与Individuals"模型具有has_many"关系.个人"模型有一个图片"上传器:

I have a "Household" model with a "has_many" relationship with an "Individuals" model. The "Individuals" model has a "picture" uploader:

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

上传的图片出现在params中:

The uploaded picture appears in the params:

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目录下.它只是从未保存到数据库中,也从未移动到文件系统中.

And is stored in the tmp directory under the upload path. It's just never saved to the database, nor moved into place in the filesystem.

有什么想法吗?

推荐答案

一些可能的解决方案:

  • 看起来你有,但只是为了确保 - 你有关于家庭模型的 accepts_nested_attributes 吗?
  • 此外,您是否尝试过,但没有局部定位问题?
  • 您在 PictureUploader 模型上安装了 Rmagick 或 minimagick 吗?

此外,您还需要注意 Carrierwave 和嵌套表单的已知问题,详见 Carrierwave维基.

And also you'll want to note of the known issue with Carrierwave and nested forms as detailed on the Carrierwave Wiki.

解决方法是添加以下方法:

The workaround is to add the method below:

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

这篇关于带有嵌套表单的 Carrierwave 上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆