0

I am trying to change the data on this request before save

Request

{"image"=>{"picture"=>[#<ActionDispatch::Http::UploadedFile:0x10c865af8 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130404-53101-1p7gv92-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"ATA.png\"\r\nContent-Type: image/png\r\n",
 @content_type="image/png",
 @original_filename="ATA.png">,
 #<ActionDispatch::Http::UploadedFile:0x10c865ad0 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130404-53101-992qy5-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"ATA@2x.png\"\r\nContent-Type: image/png\r\n",
 @content_type="image/png",
 @original_filename="ATA@2x.png">],
 "album_id"=>"10"},
 "authenticity_token"=>"00/bnTiry5OIgGsXzeWIV2FccFiIRhsxuTg/Uep32H8=",
 "commit"=>"Submit",
 "utf8"=>"✓",
 "album_id"=>"10"}

Controller

def create
    @album = Album.find(params[:album_id])
        p = Image.find(params[:image][:picture])
        p.each do |pic|
            @image = @album.images.new(params[:image][p => :picture])
        end
        if @image.save
            flash[:notice] = "Successfully added image!"
            redirect_to [:admin, @album, :images]
        else
            flash[:notice] = "failed"
        end
    end

Why does this code produce this error? If this error was resolved, would this code even work to create a new image object for every :picture array in the hash?

derek_duncan
  • 1,377
  • 1
  • 13
  • 22

1 Answers1

1

I guess it's simple typo:

p.each do |pic|
  @image = @album.images.new(params[:image][p => :picture])
end

You do a do |pic| but don't use the pic anywhere.

wintermeyer
  • 8,178
  • 8
  • 39
  • 85