ruby - Only Last Parameter being passed into Object -
my app supposed create image object each image selected in input field of form. it–the create action– iterates on :picture
param , each entry, supposed create new image object. however, seems create image object last image selected in input box. why not functioning properly?
controller
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 |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 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 |pic| # size = array_of_pictures.size.to_i # size.times {@image.build(params[:image], :picture => pic)} # @image.save # end # end end
view form
<%= form_for([:admin, :album, @image], :html => {:multipart => true}) |f| %> <%= f.hidden_field :album_id, :value => @album.id %> <%= f.file_field :picture, :multiple => true %> <%= f.submit "submit" %> <%end%>
request params on submit
{"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"}
i appreciate can share!
@album.images.build
create object, not save database. when exit each
loop, @image
last image built (which last image in params).
using @album.images.create
save image database persisted expect; however, logic after each
block won't valid anymore, since you'll verifying last image saved. should check if each image saved inside of each block, , somehow record ones unsuccessful if want do.
Comments
Post a Comment