I have a rails app. I'm using gem:paperclip to upload photos to my server. What I'm trying to do is create a folder structure on the server using information from the perams hash. Please see code below.
photo controller.rb
# POST /photos
# POST /photos.json
def create
@photo = Photo.new(params[:photo])
@photo.lat = params["photo%5Blat%5D"].to_s
@photo.lng = params["photo%5Blng%5D"].to_s
@photo.description = params[:description]
@photo.takenby = params[:takenby].to_s
@photo.save
puts "photos/create photos.inspect= #{@photo.inspect}"
end
photo.rb
class Photo < ActiveRecord::Base
attr_accessible :lat, :lng, :image
vendor = params[:description]
owner = params[:owner]
Paperclip.interpolates :prefix do |attachment, style|
"#{owner}/#{Date.today.to_s }/#{vendor}"
end
has_attached_file :image,
:path => ":prefix/:basename.:extension",
:styles => { :thumbnail => "57x57", :original => "100x100" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS
However, I'm getting this error on my server console.
Started GET "/photos?lat=37.785834&lng=-122.406417" for 127.0.0.1 at 2012-09-26
21:08:57 -0700
21:08:57 web.1 | Processing by PhotosController#index as JSON
21:08:57 web.1 | Parameters: {"lat"=>"37.785834", "lng"=>"-122.406417"}
21:08:57 web.1 | Completed 500 Internal Server Error in 4ms
21:08:57 web.1 | NameError (undefined local variable or method `params' for #<Class:0x007fb7d4fa3320>):
21:08:57 web.1 | app/models/photo.rb:23:in `<class:Photo>'
21:08:57 web.1 | app/models/photo.rb:18:in `<top (required)>'
21:08:57 web.1 | app/controllers/photos_controller.rb:14:in `index'
What do I need to change in the model so that paperclip saves the image on the server with the correct folder structure?
This solved my problem:
photo.rb
Paperclip.interpolates :prefix do |attachment, style|
"#{attachment.instance.takenby}/#{Date.today.to_s }/#{attachment.instance.description}"
end
But this introduced a new problem. I can't save different sized images anymore. How do I do that?