Advice needed: Create method for multiple file upload.

77 views
Skip to first unread message

Arve Voldsund

unread,
Oct 23, 2012, 6:22:40 PM10/23/12
to hobo...@googlegroups.com
I struggle a bit with a create method for a model with multiple file upload, and could really need some advice on this.

Basically I want the user to create a new projectfile (that's the model name) and assign it to a project. This is trivial.

Then, in the edit-page for that particular projectfile there is a file upload using the jQuery file upload plugin which allows multiple files.
Files upload just fine, but only one file will be assigned to the projectfile in the database. I understand why, but I do not quite get how to fix it in the create method.


Arve


Vivek Sampara

unread,
Oct 23, 2012, 9:49:11 PM10/23/12
to hobo...@googlegroups.com
Arve,

Can you show your params results( console )  here you get after clicking create button  ? and what is the association you specified in the projectfile and Project Model ?

Vivek 



Arve


--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/9skhe0yG4hUJ.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.

Arve Voldsund

unread,
Oct 24, 2012, 6:36:10 AM10/24/12
to hobo...@googlegroups.com
Hi Vivek,

sure!

The relationship: projectfile belongs_to project, project has_many projectfiles. 

This is the output when a "projectfile" is created:


Started POST "/projectfiles?page_path=%2Fprojectfiles%2Fnew&authenticity_token=aPKvCdlOPVGREo%2BuFgJ3ryKgIdoEFm7%2F6XKoYj1WNxY%3D&projectfile%5Bname%5D=testProjectfile&projectfile%5Bproject_id%5D=3" for 127.0.0.1 at 2012-10-24 12:30:35 +0200
Processing by ProjectfilesController#create as JS
  Parameters: {"render"=>{"0"=>{"part_context"=>"BAhbCDoPc3RhdHVzX2JveEkiCXRoaXMGOgZFRlsA--9abff3e5dffdf3c9c358f90407a981ed82647623", "id"=>"status-box"}}, "page_path"=>"/projectfiles/new", "authenticity_token"=>"aPKvCdlOPVGREo+uFgJ3ryKgIdoEFm7/6XKoYj1WNxY=", "projectfile"=>{"name"=>"testProjectfile", "project_id"=>"3"}}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
   (0.0ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "projectfiles" ("created_at", "name", "prattach_content_type", "prattach_file_name", "prattach_file_size", "prattach_updated_at", "project_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Wed, 24 Oct 2012 10:30:35 UTC +00:00], ["name", "testProjectfile"], ["prattach_content_type", nil], ["prattach_file_name", nil], ["prattach_file_size", nil], ["prattach_updated_at", nil], ["project_id", 3], ["updated_at", Wed, 24 Oct 2012 10:30:35 UTC +00:00]]
[paperclip] Saving attachments.
   (52.1ms)  commit transaction
  DRYML: Compiled app/views/projectfiles/new.dryml in 0.01s
Call part: status_box. this-id = this, locals = []
Completed 200 OK in 75ms (Views: 0.1ms | ActiveRecord: 52.7ms)


This is when files are uploaded from the "projectfile" edit-page:



Started PUT "/projectfiles/11-testprojectfile?render[0][id]=image&render[0][part_context]=BAhbCToKaW1hZ2VJIhNwcm9qZWN0ZmlsZToxMQY6BkVGWwBbBkkiEHByb2plY3RmaWxlBjsGRg==--b6689878e7b1a69e638a6e6954162adff3eb293c" for 127.0.0.1 at 2012-10-24 12:34:14 +0200


Started PUT "/projectfiles/11-testprojectfile?render[0][id]=image&render[0][part_context]=BAhbCToKaW1hZ2VJIhNwcm9qZWN0ZmlsZToxMQY6BkVGWwBbBkkiEHByb2plY3RmaWxlBjsGRg==--b6689878e7b1a69e638a6e6954162adff3eb293c" for 127.0.0.1 at 2012-10-24 12:34:14 +0200


Started PUT "/projectfiles/11-testprojectfile?render[0][id]=image&render[0][part_context]=BAhbCToKaW1hZ2VJIhNwcm9qZWN0ZmlsZToxMQY6BkVGWwBbBkkiEHByb2plY3RmaWxlBjsGRg==--b6689878e7b1a69e638a6e6954162adff3eb293c" for 127.0.0.1 at 2012-10-24 12:34:14 +0200
Processing by ProjectfilesController#update as JS
  Parameters: {"page_path"=>"/projectfiles/11-testprojectfile/edit", "authenticity_token"=>"aPKvCdlOPVGREo+uFgJ3ryKgIdoEFm7/6XKoYj1WNxY=", "projectfile"=>{"prattach"=>#<ActionDispatch::Http::UploadedFile:0x007f852a533de0 @original_filename="togeterirute.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"projectfile[prattach]\"; filename=\"togeterirute.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/ww/z22b7qzd0cd7w55798byhhzm0000gn/T/RackMultipart20121024-29919-1xjd4ou>>}, "render"=>{"0"=>{"id"=>"image", "part_context"=>"BAhbCToKaW1hZ2VJIhNwcm9qZWN0ZmlsZToxMQY6BkVGWwBbBkkiEHByb2plY3RmaWxlBjsGRg==--b6689878e7b1a69e638a6e6954162adff3eb293c"}}, "id"=>"11-testprojectfile"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
  Projectfile Load (0.1ms)  SELECT "projectfiles".* FROM "projectfiles" WHERE "projectfiles"."id" = ? LIMIT 1  [["id", "11-testprojectfile"]]
Processing by ProjectfilesController#update as JS
  Parameters: {"page_path"=>"/projectfiles/11-testprojectfile/edit", "authenticity_token"=>"aPKvCdlOPVGREo+uFgJ3ryKgIdoEFm7/6XKoYj1WNxY=", "projectfile"=>{"prattach"=>#<ActionDispatch::Http::UploadedFile:0x007f852eddc218 @original_filename="QR.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"projectfile[prattach]\"; filename=\"QR.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/ww/z22b7qzd0cd7w55798byhhzm0000gn/T/RackMultipart20121024-29924-lnxop8>>}, "render"=>{"0"=>{"id"=>"image", "part_context"=>"BAhbCToKaW1hZ2VJIhNwcm9qZWN0ZmlsZToxMQY6BkVGWwBbBkkiEHByb2plY3RmaWxlBjsGRg==--b6689878e7b1a69e638a6e6954162adff3eb293c"}}, "id"=>"11-testprojectfile"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
  Projectfile Load (0.1ms)  SELECT "projectfiles".* FROM "projectfiles" WHERE "projectfiles"."id" = ? LIMIT 1  [["id", "11-testprojectfile"]]
Command :: identify -format %wx%h :file
Command :: identify -format %wx%h :file
Processing by ProjectfilesController#update as JS
  Parameters: {"page_path"=>"/projectfiles/11-testprojectfile/edit", "authenticity_token"=>"aPKvCdlOPVGREo+uFgJ3ryKgIdoEFm7/6XKoYj1WNxY=", "projectfile"=>{"prattach"=>#<ActionDispatch::Http::UploadedFile:0x007f852b50d270 @original_filename="IMG_4260.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectfile[prattach]\"; filename=\"IMG_4260.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/ww/z22b7qzd0cd7w55798byhhzm0000gn/T/RackMultipart20121024-29926-h35lak>>}, "render"=>{"0"=>{"id"=>"image", "part_context"=>"BAhbCToKaW1hZ2VJIhNwcm9qZWN0ZmlsZToxMQY6BkVGWwBbBkkiEHByb2plY3RmaWxlBjsGRg==--b6689878e7b1a69e638a6e6954162adff3eb293c"}}, "id"=>"11-testprojectfile"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
  Projectfile Load (0.1ms)  SELECT "projectfiles".* FROM "projectfiles" WHERE "projectfiles"."id" = ? LIMIT 1  [["id", "11-testprojectfile"]]
Command :: identify -format %wx%h :file
Command :: convert :source -auto-orient -resize "x138" -crop "200x138+19+0" +repage :dest
Command :: convert :source -auto-orient -resize "x138" -crop "200x138+29+0" +repage :dest
Command :: convert :source -auto-orient -resize "200x" -crop "200x138+0+31" +repage :dest
Command :: identify -format %wx%h :file
Command :: convert :source -auto-orient -resize "100x100>" :dest
Command :: identify -format %wx%h :file
Command :: convert :source -auto-orient -resize "100x100>" :dest
Command :: identify -format %wx%h :file
   (0.2ms)  begin transaction
   (0.4ms)  UPDATE "projectfiles" SET "prattach_file_name" = 'QR.png', "prattach_file_size" = 192785, "prattach_updated_at" = '2012-10-24 10:34:14.981083', "updated_at" = '2012-10-24 10:34:15.153576' WHERE "projectfiles"."id" = 11
[paperclip] Saving attachments.
[paperclip] deleting lib/prattach/original/togeterirute.png
[paperclip] deleting lib/prattach/medium/togeterirute.png
[paperclip] deleting lib/prattach/thumb/togeterirute.png
   (2.5ms)  commit transaction
Command :: convert :source -auto-orient -resize "100x100>" :dest
Call part: image. this-id = projectfile:11, locals = []
         : form_field_path = ["projectfile"]
Completed 200 OK in 203ms (Views: 0.3ms | ActiveRecord: 3.4ms)
   (0.2ms)  begin transaction
   (0.4ms)  UPDATE "projectfiles" SET "prattach_file_name" = 'IMG_4260.jpg', "prattach_content_type" = 'image/jpeg', "prattach_file_size" = 98787, "prattach_updated_at" = '2012-10-24 10:34:15.000437', "updated_at" = '2012-10-24 10:34:15.195829' WHERE "projectfiles"."id" = 11
[paperclip] Saving attachments.
[paperclip] deleting lib/prattach/original/togeterirute.png
[paperclip] deleting lib/prattach/medium/togeterirute.png
[paperclip] deleting lib/prattach/thumb/togeterirute.png
   (2.3ms)  commit transaction
   (0.2ms)  begin transaction
Call part: image. this-id = projectfile:11, locals = []
         : form_field_path = ["projectfile"]
   (0.5ms)  UPDATE "projectfiles" SET "prattach_updated_at" = '2012-10-24 10:34:14.978709', "updated_at" = '2012-10-24 10:34:15.212436' WHERE "projectfiles"."id" = 11
[paperclip] Saving attachments.
[paperclip] deleting lib/prattach/original/togeterirute.png
[paperclip] deleting lib/prattach/medium/togeterirute.png
Completed 200 OK in 231ms (Views: 0.3ms | ActiveRecord: 3.2ms)
[paperclip] deleting lib/prattach/thumb/togeterirute.png
   (2.5ms)  commit transaction
  DRYML: Compiled app/views/projectfiles/edit.dryml in 0.03s
Call part: image. this-id = projectfile:11, locals = []
         : form_field_path = ["projectfile"]
Completed 200 OK in 293ms (Views: 0.2ms | ActiveRecord: 3.5ms)

Vivek Sampara

unread,
Oct 24, 2012, 1:50:56 PM10/24/12
to hobo...@googlegroups.com
Arve,

There are multiple form posts happening. are you trying to post all the files together or doing it from /new page multiple times ? 

If you are dealing with file types a lot, id suggest you to use dragonfly gem. I have used it recently and its easier than paperclip . 


and follow Florians advice here on multiple files in a single upload here 


Vivek 

To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/n_ITLJzDsbwJ.

Arve Voldsund

unread,
Oct 24, 2012, 4:30:09 PM10/24/12
to hobo...@googlegroups.com
Thanks! I post all files together with paperclip and the jQuery file upload plugin. 

I'll have a look at dragonfly! Thanks for the links!


Arve

Bryan Larsen

unread,
Oct 24, 2012, 9:59:59 PM10/24/12
to hobo...@googlegroups.com
On Wed, Oct 24, 2012 at 6:36 AM, Arve Voldsund <a.vol...@gmail.com> wrote:
> Hi Vivek,
>
> sure!
>
> The relationship: projectfile belongs_to project, project has_many
> projectfiles.
>

I assume it is that projectfile

has_attached_file :prattach

That should work. Can you post your DRYML? The default form should
use an input_many for projectfiles, so it should just work out of the
box. I haven't tried it recently, but it has worked in the past.

Bryan

Ignacio Huerta

unread,
Oct 25, 2012, 3:09:00 PM10/25/12
to hobo...@googlegroups.com
Hi Arve,

I'm a bit confused about your models and relationships. If you want to
have several attachments to a project, then you are gonna need a 1-N
relationship:

A project has many projectfiles
A projectfile belongs to one project

Maybe I got the idea wrong, but you are trying to create a multiple
file upload in the edit page of a projectfile. I think it would make
more sense to do it in the edit page of the project. Or maybe I didn't
understand your relationships :P.

By the way, there's a working example of multiple file upload (you can
drag 3 attachments in the show of a project) in here:
https://github.com/suyccom/sgagility . I'm afraid I haven't taken much
care of that app (I plan to replace it soon with a cleaner one), and
some parts might be broken, but it might serve as an example.

Regards,
Ignacio

El 24/10/12 00:22, Arve Voldsund escribi�:
> -- You received this message because you are subscribed to the
> Google Groups "Hobo Users" group. To view this discussion on the
> web visit
> https://groups.google.com/d/msg/hobousers/-/9skhe0yG4hUJ. To post
> to this group, send email to hobo...@googlegroups.com. To
> unsubscribe from this group, send email to
> hobousers+...@googlegroups.com. For more options, visit
> this group at http://groups.google.com/group/hobousers?hl=en.

--
Ignacio Huerta Arteche
http://www.ihuerta.net
Tel�fono: 0034 645 70 77 35
Email realizado con software libre

Arve Voldsund

unread,
Oct 26, 2012, 6:30:43 AM10/26/12
to hobo...@googlegroups.com
Hi Ignacio,

The sgagility project has been tremendous help for me in trying to get a grip of how things work. I find it a bit hard to understand all the flexibility that lies within hobo, and sgagility sort of opened up a few doors so to say. I first started using rails when I found Hobo and was pretty fresh at Ruby as well, so there has been a few things to sort out, but it is coming together now.

To the point:

What I wanted was to have a project that is merely a description of what kind of data the project represents, and then files should be attached to this project as a set belonging to a projectfile. In other words: The projectfile should be a container that keeps track of which uploads that belong to a certain project.

I understood from your comments (thanks) that I missed an abstraction layer, and changed the relationships as follows:

Project has has many projectfiles and projectfiles has many uploads.

The idea behind this is that any project can be created and assigned to any set of projectfiles, and then the project needs to own the projectfile container which again owns the uploads.

The uploads are created from the projectfile edit-page (since it is required to create a projectfile before uploading).

It is all working now, and the controller for projectfiles looks like this, and I'd be glad to get feedback on it if it should be done in another way:

class ProjectfilesController < ApplicationController

hobo_model_controller

auto_actions :all

def index
hobo_index do
@projectfile = Projectfile.new
end
end

def show
hobo_show do
if params[:style]
send_file @projectfile.prattach.path(params[:style])
else
render
end
end
end

def update
hobo_update do
Upload.create(
:file_name =>this.prattach_file_name,
:file_type =>this.prattach_content_type,
:file_size =>this.prattach_file_size,
:file_created =>this.prattach_updated_at,
:projectfile_id => this.id
)

respond_to do |format|
format.js { hobo_ajax_response }
format.html { redirect_to @projectfile }
end
end
end

end



Best,
Arve

On Oct 25, 2012, at 9:09 PM, Ignacio Huerta <ign...@ihuerta.net> wrote:

> Hi Arve,
>
> I'm a bit confused about your models and relationships. If you want to
> have several attachments to a project, then you are gonna need a 1-N
> relationship:
>
> A project has many projectfiles
> A projectfile belongs to one project
>
> Maybe I got the idea wrong, but you are trying to create a multiple
> file upload in the edit page of a projectfile. I think it would make
> more sense to do it in the edit page of the project. Or maybe I didn't
> understand your relationships :P.
>
> By the way, there's a working example of multiple file upload (you can
> drag 3 attachments in the show of a project) in here:
> https://github.com/suyccom/sgagility . I'm afraid I haven't taken much
> care of that app (I plan to replace it soon with a cleaner one), and
> some parts might be broken, but it might serve as an example.
>
> Regards,
> Ignacio
>
> El 24/10/12 00:22, Arve Voldsund escribió:
>> I struggle a bit with a create method for a model with multiple
>> file upload, and could really need some advice on this.
>>
>> Basically I want the user to create a new projectfile (that's the
>> model name) and assign it to a project. This is trivial.
>>
>> Then, in the edit-page for that particular projectfile there is a
>> file upload using the jQuery file upload plugin which allows
>> multiple files. Files upload just fine, but only one file will be
>> assigned to the projectfile in the database. I understand why, but
>> I do not quite get how to fix it in the create method.
>>
>>
>> Arve
>>
>>
>> -- You received this message because you are subscribed to the
>> Google Groups "Hobo Users" group. To view this discussion on the
>> web visit
>> https://groups.google.com/d/msg/hobousers/-/9skhe0yG4hUJ. To post
>> to this group, send email to hobo...@googlegroups.com. To
>> unsubscribe from this group, send email to
>> hobousers+...@googlegroups.com. For more options, visit
>> this group at http://groups.google.com/group/hobousers?hl=en.
>
> --
> Ignacio Huerta Arteche
> http://www.ihuerta.net
> Teléfono: 0034 645 70 77 35
> Email realizado con software libre
>
> --
> You received this message because you are subscribed to the Google Groups "Hobo Users" group.

Ignacio Huerta

unread,
Oct 31, 2012, 1:53:26 PM10/31/12
to hobo...@googlegroups.com
Hi Arve,

Sory for the late response. I'm glad sgagility worked for you as an
example. I've looked at your code and it looks very nice.

From your explanation, you might wanna take a look at using a
many-to-many relatinship between projects and project files, but I'm not
really sure if you need that. You have an example in the agility
tutorial if you do.

Regards,
Ignacio

El 26/10/12 12:30, Arve Voldsund escribi�:
>> El 24/10/12 00:22, Arve Voldsund escribi�:
>>> I struggle a bit with a create method for a model with multiple
>>> file upload, and could really need some advice on this.
>>>
>>> Basically I want the user to create a new projectfile (that's the
>>> model name) and assign it to a project. This is trivial.
>>>
>>> Then, in the edit-page for that particular projectfile there is a
>>> file upload using the jQuery file upload plugin which allows
>>> multiple files. Files upload just fine, but only one file will be
>>> assigned to the projectfile in the database. I understand why, but
>>> I do not quite get how to fix it in the create method.
>>>
>>>
>>> Arve
>>>
>>>
>>> -- You received this message because you are subscribed to the
>>> Google Groups "Hobo Users" group. To view this discussion on the
>>> web visit
>>> https://groups.google.com/d/msg/hobousers/-/9skhe0yG4hUJ. To post
>>> to this group, send email to hobo...@googlegroups.com. To
>>> unsubscribe from this group, send email to
>>> hobousers+...@googlegroups.com. For more options, visit
>>> this group at http://groups.google.com/group/hobousers?hl=en.
>>
>> --
>> Ignacio Huerta Arteche
>> http://www.ihuerta.net
>> Tel�fono: 0034 645 70 77 35
>> Email realizado con software libre
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Hobo Users" group.
>> To post to this group, send email to hobo...@googlegroups.com.
>> To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
>> For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
>>
>

--
Ignacio Huerta Arteche
http://www.ihuerta.net
Tel�fono: 0034 645 70 77 35

Arve Voldsund

unread,
Nov 1, 2012, 6:16:28 PM11/1/12
to hobo...@googlegroups.com
Hi Ignacio,

Thanks! I don't think I need a many to many in this app, but that might change. It's a massive amount of files in each project so I guess I'll just have to review as I go...

I had to add paperclip to the uploads model, as I couldn't get the routing to the files right when I just copied file info to the uploads model. I think that would've been cleaner but it doesn't matter at all.

Ended up with this:

def update

hobo_update do
Upload.create(
:attachment=>this.prattach,
:name=>this.prattach_file_name,
:attachment_file_name =>this.prattach_file_name,
:attachment_content_type =>this.prattach_content_type,
:attachment_file_size =>this.prattach_file_size,
:attachment_updated_at =>this.prattach_updated_at,
:projectfile_id => this.id
)
respond_to do |format|
format.js { hobo_ajax_response }
format.html { redirect_to @projectfile }
end
end
end



Arve
On Oct 31, 2012, at 6:53 PM, Ignacio Huerta <ign...@ihuerta.net> wrote:

> Hi Arve,
>
> Sory for the late response. I'm glad sgagility worked for you as an example. I've looked at your code and it looks very nice.
>
> From your explanation, you might wanna take a look at using a many-to-many relatinship between projects and project files, but I'm not really sure if you need that. You have an example in the agility tutorial if you do.
>
> Regards,
> Ignacio
>
> El 26/10/12 12:30, Arve Voldsund escribió:
>>> El 24/10/12 00:22, Arve Voldsund escribió:
>>>> I struggle a bit with a create method for a model with multiple
>>>> file upload, and could really need some advice on this.
>>>>
>>>> Basically I want the user to create a new projectfile (that's the
>>>> model name) and assign it to a project. This is trivial.
>>>>
>>>> Then, in the edit-page for that particular projectfile there is a
>>>> file upload using the jQuery file upload plugin which allows
>>>> multiple files. Files upload just fine, but only one file will be
>>>> assigned to the projectfile in the database. I understand why, but
>>>> I do not quite get how to fix it in the create method.
>>>>
>>>>
>>>> Arve
>>>>
>>>>
>>>> -- You received this message because you are subscribed to the
>>>> Google Groups "Hobo Users" group. To view this discussion on the
>>>> web visit
>>>> https://groups.google.com/d/msg/hobousers/-/9skhe0yG4hUJ. To post
>>>> to this group, send email to hobo...@googlegroups.com. To
>>>> unsubscribe from this group, send email to
>>>> hobousers+...@googlegroups.com. For more options, visit
>>>> this group at http://groups.google.com/group/hobousers?hl=en.
>>>
>>> --
>>> Ignacio Huerta Arteche
>>> http://www.ihuerta.net
>>> Teléfono: 0034 645 70 77 35
>>> Email realizado con software libre
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "Hobo Users" group.
>>> To post to this group, send email to hobo...@googlegroups.com.
>>> To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
>>> For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
>>>
>>
>
> --
> Ignacio Huerta Arteche
> http://www.ihuerta.net
> Teléfono: 0034 645 70 77 35
Reply all
Reply to author
Forward
0 new messages