7

私は近い...非常に近い...単一のファイルをうまくアップロードできます...しかし、フォームのタイプをに変更すると、file_field一度:multiple => trueに複数の画像をアップロードできるようになります。アップロードしたファイルは配列...そして「accepts_nested_attributes_for」の「魔法」が失われます。

編集: さらに調べた後、気にする必要があるのだろうaccepts_nested_attributes_forか?おそらくfile_field, :multiple => true、(ネストされたフォームとは対照的に) ギャラリー フォームに を入れてから、作成アクションで新しいギャラリーを作成し、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

accept_nested_attributes_for を捨てて、代わりにこれをギャラリー モデルに追加します。

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

また、一括割り当てから保護する場合に備えて、写真がギャラリーのアクセス可能な属性にあることを確認してください。そうしないと、params から photos 配列のハッシュ割り当てを取得できません。いえ

attr_accessible :field1, field2, :photos
于 2011-07-07T14:46:27.900 に答える