0

I have been stuck on a problem for three weeks now, and although I have tried everyday to find an answer to my problem, I haven't found anything that I have been able to make work. If anyone could help me, I would be very, very grateful.

I have built a basic blog in Rails that allows an admin user to publish articles using a form, containing a title text_field, body text_area, and file_field for an image, which uses Paperclip. All very simple and easy and no problems there. However, I have been trying to alter the form to allow an admin user to upload multiple images as part of a new article, but have been unable to find a way to do this.

I need a solution that will allow me to achieve multiple image upload functionality in a form. I have tried many gems and many tutorials, but have been unable to integrate anything into my app without completely breaking it. Can any one help me get out of this dead end I've found myself in for the last three weeks? Do you have any clear tutorials or solutions that will help me overcome this problem?

Thank you very much to anyone that can help me.

2 Answers2

1

Your question is quite broad, without any schema or anything so I'll try to help you as much as possible given that.

If it's a blog, you might have a post model. You could have a photo model, with a reference to the post model. If you add paperclip to your photo model, then you can save multiple photos.

There are multiple ways to do this. The first one I think of is using nested forms with a gem like cocoon.

The second I think of is using a dropzone, which would upload the photos using Ajax.

Graham Slick
  • 6,692
  • 9
  • 51
  • 87
0

By default PaperClip allows you to upload one attachment. However, a clean way I've found to work around this is to create another model which would wrap around the images.

class Article < AR::Base
   has_many :images
   accepts_nested_attributes_for :images
end

class Image < AR::Base
   belongs_to :article
   has_attached_file :filename, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
   validates_attachment_content_type :filename, content_type: /\Aimage\/.*\Z/
end

Don't forget to run a migration to create the images table:

rails generate model image article:references

and

rails generate paperclip image filename

Then in your articles/form, where you initially had the field to upload the image, you would have

<%= f.fields_for :images do |p| %>
  <%= p.label :filename %><br>
  <%= p.file_field :filename%>
<% end %>

In your articles_controller#article_params, along with the current params, you would have, params.require(:article).permit(..., images_attributes: [:filename])

Afterwards, allowing the uploads of multiple images would therefore only require the basic knowledge of nested forms. To set a default number of file_fields, in your ArticlesController#new after initializing your @article you would have

def new
  @article = Article.new
  2.times{ @article.images.build}
end

With this setup, when you navigate to your new_articles route, you should see your article ready to upload 2 images. To learn more about how to use NestedForms to do even more, you should see Ryan Bates tutorial. This approach provides a cleaner interface, as the other approaches, I guess, would require some sort of hack.

Let me know if I'm able to help or further clarifications required.

oreoluwa
  • 5,553
  • 2
  • 20
  • 27
  • Hi, and thanks for the detailed reply. I have worked through your solution and it doesn't give me any errors. However, it doesn't return any images for me, just empty arrays. I have paperclip set up with S3, but it doesn't seem to be working. I am literally at my end with this project. It has been impossible for me to find a thorough guide on how to implement this function for nearly four weeks now. Lots of people have tried to be helpful and give me hints, and I say thank you to everyone of them, but unless I can find some step by step guide to achieve this, I feel that I never will. – Frazer Watson Jun 09 '16 at 20:21
  • Happy, I was able to help. I'd be happy to walk you through! If the project is on Github, I could help look through to provide some more insights. Or let me know any other method that suites you. Github: github.com/andela-oakinniranye – oreoluwa Jun 09 '16 at 21:44