New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Append more files #1545
Comments
@fabrouy, It think it seems odd to use the << method to add an array to another. Something like u = User.find(xxx)
u.images += params[:files]
u.save! is more intuitive. (It may already work, I have no idea). |
Hello. I have a question. When i try do like this
then in tmp directory created all files which have already been in the array Sorry for my english |
@Uelb In fact |
<< implies appending a single element to a collection whereas += appends a collection to another one. [1,2,3] << [4,5,6] # => [1,2,3, [4,5,6]]
[1,2,3] += [4,5,6] # => [1,2,3,4,5,6] |
@Uelb this is true, so in fact the right API would be # Using << appends a single item to an existing array.
arr = [1,2,3] #=> [1, 2, 3]
arr.object_id #=> 10564640
arr << 4 #=> [1, 2, 3, 4]
arr.object_id #=> 10564640
# Using += creates a new array.
arr = [1,2,3] #=> [1, 2, 3]
arr.object_id #=> 11147760
arr += [4] #=> [1, 2, 3, 4]
arr.object_id #=> 11277560
# Using #concat can append multiple items.
arr = [1,2,3] #=> [1, 2, 3]
arr.object_id #=> 11329480
arr.concat([4]) #=> [1, 2, 3, 4]
arr.object_id #=> 11329480 |
I totally agree with you :D ! |
class Author < ActiveRecord::Base
mount_uploaders :photos, AuthorPhotoUploader
# ...
end author.photos.concat(get_photos)
author.save i get error: NoMethodError (undefined method `identifier' for #ActionDispatch::Http::UploadedFile:0x0000000a727950) |
Hi @evg2108, I never said that |
But I still don't know how do this right. Do you have any suggestion? |
When will be |
I managed to append images. My use case is a post model with an images field. The code changes are: post.rb
post_controller.rb
post_controller.rb (private)
Hope it helps |
@juanbrein Thank you. My solution is Post has many Image model and use class Post < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images, reject_if: :all_blank, allow_destroy: true
end
class Image < ActiveRecord::Base
mount_uploader :file, ImageUploader
end
class PostsController < ApplicationController
private
def post_params
params.require(:post).permit(:title, :imagess_attributes => [:id, :file, :_destroy]}
end
end But these solutions are little complicated. I really hope |
@juanbrein when you append new images all old images will be re-processed. And appending every new image will be slower. I change your code: in model def append_images post_params
appended_images = post_params[:images].map do |image|
uploader = PostImageUploader.new(self)
uploader.store! image
uploader
end
self[:images] ||= []
self[:images] += appended_images.map{ |uploader| uploader.file.filename }
end in controller @post.append_images post_params if post_params[:images]
@post.save |
I've written a test to demonstrate the issue unmanbearpig@972945a I think the biggest problem is not that it's not possible to use |
I solved this issue by adding this to the model with the images attached. I am a novice coder so I can't promise it is the fastest, but it does work. I have the images stored as JSON.
|
Closed by #2401. |
Hi guys,
This is what I have:
:images
is stored as json in the DB.Also I am sending files in chunks using dropzone, so for example If I drop two images at the same time they get stored together in the json field.
The problem comes when I want to drop another image after waiting a while, I would like to "append" this new image to the other two in an update action.
I would like to do something like this:
Is this possible to accomplish?
The text was updated successfully, but these errors were encountered: