rails访问ActionDispatch错误

时间:2013-04-05 02:26:44

标签: ruby ruby-on-rails-3.2 actiondispatch

我正在尝试在保存

之前更改此请求的数据

请求

{"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"}

控制器

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

为什么此代码会产生此错误?如果此错误得到解决,此代码是否可以为散列中的每个:picture数组创建新的图像对象?

1 个答案:

答案 0 :(得分:1)

我想这很简单:

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

您执行do |pic|但不要在任何地方使用pic