7

我很接近...非常接近...我可以上传单个文件就好了...但是当我将表单的类型更改file_field:multiple => true这样我可以一次上传多个图像时,我上传的文件被包裹在一个数组......并且“accepts_nested_attributes_for”的“魔法”丢失了。

编辑: 经过更多检查,我想知道我是否需要打扰accepts_nested_attributes_for?也许我应该file_field, :multiple => true在我的 Gallery 表单(而不是嵌套表单)中有一个,然后在我的 create 操作中创建新的画廊,然后params[:gallery][:photos_attributes]["0"][:image]手动循环遍历数组中的每个元素,可以这么说,调用@gallery.photos.create每个元素。?!?看起来很麻烦……但这是我能想到的。

希望有更多Rails经验的人能加入...

参数:

{"utf8"=>"✓", 
"authenticity_token"=>"9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c=", 
"gallery"=>{
  "name"=>"First Gallery", 
  "photos_attributes"=>{"0"=>{
    "image"=>[
      #<ActionDispatch::Http::UploadedFile:0x00000104b78978 
        @original_filename="first_test_image.jpg", 
        @content_type="image/jpeg", 
        @headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"first_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n", 
        @tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-vz78ee>>, 
      #<ActionDispatch::Http::UploadedFile:0x00000104b78950 
        @original_filename="second_test_image.jpg", 
        @content_type="image/jpeg", 
        @headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"second_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n", 
        @tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-1jzhhyg>>
      ]
    }
  }
}, "commit"=>"Save", "action"=>"create", "controller"=>"galleries"}



#app/models/gallery.rb 
class Gallery < ActiveRecord::Base 
  has_many :photos, :dependent => :destroy 
  accepts_nested_attributes_for :photos 
end 

#app/models/photo.rb 
class Photo < ActiveRecord::Base 
  belongs_to :gallery 
  mount_uploader :photo, PhotoUploader 
end 

#config/routes.rb
resources :galleries do
  resources :photo, :only => [:create, :destroy]
end

画廊控制器

  def new
    @gallery = Gallery.new
    @gallery.photos.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @gallery }
    end
  end

  ...

  def create
    @gallery = Gallery.new(params[:gallery])

    respond_to do |format|
      if @gallery.save
        format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
        format.json { render json: @gallery, status: :created, location: @gallery }
      else
        format.html { render action: "new" }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end
4

2 回答 2

4

您可以对 params 数组进行一些修改,例如:

aux = []
params[:gallery][:photos_attributes][:image].each do |f|
  aux << {:image => f}
end
params[:post][:photos_attributes] = aux

@gallery = Gallery.new(params[:gallery])

我有一个类似的问题,我知道这是一个丑陋的黑客,但它对我有用。

于 2011-11-16T03:22:15.217 回答
2

抛弃接受_nested_attributes_for 并将其添加到您的图库模型中。

def photos=(attrs)
  attrs.each { |attr| self.photos.build(:image => attr) }
end

此外,请确保照片在您画廊的可访问属性中,以防您防止批量分配。否则,您将无法从您的参数中获得照片数组哈希分配。IE

attr_accessible :field1, field2, :photos
于 2011-07-07T14:46:27.900 回答