我有一个轨道应用程序。我正在使用 gem:paperclip 将照片上传到我的服务器。我要做的是使用来自 perams 哈希的信息在服务器上创建一个文件夹结构。请看下面的代码。
照片控制器.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
照片.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
但是,我在服务器控制台上收到此错误。
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'
我需要在模型中进行哪些更改,以便回形针以正确的文件夹结构将图像保存在服务器上?
这解决了我的问题:
photo.rb
Paperclip.interpolates :prefix do |attachment, style|
"#{attachment.instance.takenby}/#{Date.today.to_s }/#{attachment.instance.description}"
end
但这引入了一个新问题。我不能再保存不同尺寸的图像了。我怎么做?