仅将最后一个参数传递给Object

时间:2013-04-05 04:23:07

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

我的应用程序应该为我在表单的输入字段中选择的每个图像创建一个图像对象。它 - 创建动作 - 迭代:picture参数,对于每个条目,它应该创建一个新的图像对象。但是,它似乎只为输入框中选择的最后一个图像创建一个图像对象。为什么这不能正常运作?

控制器

class Admin::ImagesController < ApplicationController
    respond_to :html, :json
    #before_filter :split_hash, :only => [ :create, :update ]
    def index
        @album = Album.find(params[:album_id])
        @images = @album.images.all
    end
    def new
        @album = Album.find(params[:album_id])
        @image = @album.images.new
    end
    def create
        params[:image][:picture].each do |image|
            @album = Album.find(params[:album_id])
            @params = {}
            @params['picture'] = image
            @image = @album.images.build(@params)
       end
        if @image.save
            flash[:notice] = "Successfully added image!"
            redirect_to [:admin, @album, :images]
        else
            render "new"
            flash[:notice] = "Did not successfully add image :("
        end
    end
    def show
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
    end
    def edit
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
    end
    def update
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
        if @image.update_attributes(params[:image])
            flash[:notice] = "Successfully updated Image"
            redirect_to [:admin, @album, :images]
        else
            render "edit"
        end
    end
    def destroy
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
        @image.destroy
        @albumid = @album.id
        @id = @image.id
        FileUtils.remove_dir("#{Rails.root}/public/uploads/image/picture/#{@albumid}/#{@id}", :force => true)
        redirect_to admin_album_images_path(@album)
    end
  #     def split_hash
  #         @album = Album.find(params[:album_id])
        # @image = @album.images
  #     array_of_pictures = params[:image][:picture]
  #     array_of_pictures.each do |pic|
  #         size = array_of_pictures.size.to_i
  #         size.times {@image.build(params[:image], :picture => pic)}
  #         @image.save
        # end
  #     end
end

查看表单

<%= form_for([:admin, :album, @image], :html => {:multipart => true}) do |f| %>
        <%= f.hidden_field :album_id, :value => @album.id %>
        <%= f.file_field :picture, :multiple => true %>
        <%= f.submit "Submit" %>
<%end%>

请求提交参数

{"image"=>{"picture"=>[#<ActionDispatch::Http::UploadedFile:0x10c986d88 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130404-53101-3c2whv-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"background-pic.jpg\"\r\nContent-Type: image/jpeg\r\n",
 @content_type="image/jpeg",
 @original_filename="background-pic.jpg">,
 #<ActionDispatch::Http::UploadedFile:0x10c986d60 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130404-53101-bvdysw-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"bible-banner.png\"\r\nContent-Type: image/png\r\n",
 @content_type="image/png",
 @original_filename="bible-banner.png">],
 "album_id"=>"10"},
 "authenticity_token"=>"dr8GMCZOQo4dQKgkM4On2uMs8iORQ68vokjW0e4VvLY=",
 "commit"=>"Submit",
 "utf8"=>"✓",
 "album_id"=>"10"}

我非常感谢您可以分享的任何帮助!

1 个答案:

答案 0 :(得分:1)

@album.images.build只会创建对象,不会将其保存到数据库中。当您退出each循环时,@image只是最后构建的图像(这将是参数中的最后一个图像)。

使用@album.images.create实际上会将图像保存到数据库中,以便它像您期望的那样持久化;但是,each块之后的逻辑将不再有效,因为您只会验证最后一张图像是否已保存。您应检查每个图像是否保存在每个块内,并以某种方式记录哪些图像不成功,如果这是您想要做的。