1

I am using Paperclip to upload multiple images and store it in s3. So, I have a gallery model, which looks like this :

class Gallery < ActiveRecord::Base
    attr_accessible :title, :body, :pictures_attributes
    has_many :pictures
    accepts_nested_attributes_for :pictures, :allow_destroy => true


end

and gallery should have many picture. My picture model looks like this :

class Picture < ActiveRecord::Base
 belongs_to :gallery
   has_attached_file :picture, :styles => { :small => "150x150>", :medium => "300x300"  },
                      :storage => :s3,
                      :s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
                      :path => "/:class/:style/:id/:filename"                 

      validates_attachment_presence :picture
      validates_attachment_size :picture, :less_than => 5.megabytes
      validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/png']

end

I already put this in my _form.html.erb :

<%= form_for @gallery, :html => { :multipart => true } do |f| %>

and this too

<%= f.fields_for :picture do |picture_form| %>
        <p>
          <%= picture_form.file_field :picture %>
        </p>
 <% end %>

In my galleries_controller, I have this :

def new
        @gallery = Gallery.new
        5.times{ @gallery.pictures.build }
    end

      # GET /galleries/1/edit
    def edit
        @gallery = Gallery.find(params[:id])
        5.times{ @gallery.pictures.build }
    end

      # POST /galleries
      # POST /galleries.xml
    def create
      @gallery = Gallery.new(params[:gallery])
      respond_to do |format|
          if @gallery.save
            format.html { redirect_to(admin_gallery_path(@gallery), :notice => 'Gallery was successfully created.') }
            format.xml  { render :xml => @gallery, :status => :created, :location => @gallery }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @gallery.errors, :status => :unprocessable_entity }
          end

        end
end

I found some similar case, followed the answer. But I still got the same error message. I tried to change RAILS_ROOT to Rails.root, but it didn't help. I tried to follow this answer, but I am not sure where do I pass the params to the paperclip?

Anyone knows what the problem is? Thanks

Community
  • 1
  • 1
ishwr
  • 725
  • 3
  • 14
  • 36
  • just something i have noticed in your paperclip config...where is your bucket? where are you actually saving the images? you need to create a bucket within your aws accoount – Richlewis May 08 '13 at 10:10
  • Do you mean this: :storage => :s3, :s3_credentials => "#{Rails.root}/config/amazon_s3.yml" I am not sure what do you mean by bucket. I am sorry. But I used the paperclip and s3 in other Model (not nested_form) and it works – ishwr May 08 '13 at 10:13
  • mean what? think you have missed something there – Richlewis May 08 '13 at 10:15
  • yeah sorry, just edited it – ishwr May 08 '13 at 10:15

1 Answers1

0

Ok so from what i can see you seem to be missing a (bucket) place to store your pictures within AWS. Ill show you an example of what i mean

 has_attached_file :avatar, 
 :styles => {:thumb => "100x100>" },
 :storage => :s3,
 :s3_credentials => "#{Rails.root}/config/s3.yml",
 :path => "/images/:id/:style.:extension",
 :url  => ":s3_domain_url",
 :bucket => "assets.recipesapp"

You create the bucket within your AWS account

Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • Eventhough in my lib>paperclip>storage I already have s3.rb, I still have to add the :bucket in paperclip config? Beacuse in other classes I also use paperclip, without adding the bucket – ishwr May 08 '13 at 10:24
  • ok so how do you then view these images via AWS, as in can you log into your account and see all your assets? This is how i do it and have had no issues in the past, try it maybe? – Richlewis May 08 '13 at 10:26
  • that's not the problem, unfortunately :( I think I know now what the problem is. I already put this <%= form_for(@gallery, :html => { :multipart => true }) do |f| %> in my _form.html.erb when I see 'view page source', I see the multipart => true but when I 'inspect element', I don't see the multipart. – ishwr May 09 '13 at 03:32
  • if your using form_for you dont need to set multipart => true, this is already done for you http://guides.rubyonrails.org/form_helpers.html#uploading-files if you where using a form_tag then yes you would need to use it – Richlewis May 09 '13 at 07:02