Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Upload photos with Jquery File Upload as part of a Post. Rails 4.

I am wondering what is the best way to upload multiple photos as part of a Post with a Jquery File Upload in Rails application.

The problem is, that when you upload photos with jquery fileupload, the post wasn't created yet.

Is it a good approach to make a JS and create the Post with JS before you start uploading files?

For example: Create post with state "waiting" with one click action(so that no one can even know that the post was created). And when the Post is submitted for real, than only change the state to "published"?

I know there is a risk that people let some posts unfinished, but I think that I can delete them after 48hours or so, or detect it somehow...

What is your opinion? Do you have any better way to do this?

PS: I need to let users know how the progress is going, because some large files are going to be uploaded....

Thank you for your opinions guys!!!

1 Answer

Charles Smith
Charles Smith
7,575 Points

Here is a tutorial just using Rails Form helpers. If you went with that method, you could just call the upload function before the create action on your post.

Here is another example. Essentially, you're sort of correct. They chose to upload the files and then submit as well.

Thank you for your answer and resources. I read those articles really quick(will do better in few hours). But is there anywhere really an example of how to solve the thing when Photos(uploaded files) belongs_to something(Post)?

I mean, you need to have the parent (Post) created, before you start uploading Photos on your server(new one is created when you select them - but if there is no parent than no parent_id can be written there...). I will definitely read those articles better in few hours, so sorry if it's written there. :)

Charles Smith
Charles Smith
7,575 Points

I think you can begin the upload without having a post created, it just won't be part of the DB until you create the post. It would be much like the first post I linked, except the upload function would return a file name to be written as part of a new post.