Rails: uploading files with paperclip

眉间皱痕 提交于 2019-12-13 14:44:25

问题


I would like to use paperclip to upload files. With the basic out of the box settings, I was able to get the file uploaded to the default directory (something in public/systems...) However when I tried changing the url or path (or both):

class Cvit < ActiveRecord::Base
    has_attached_file :fileup, :path => ":rails_root/public/data/01_fasta"
end

I lose permission to the 01_fasta directory, after doing a chmod 777 on it, I notice the file is there but its named something like, stream20110706-45944-12lt2oo-0

also tried #{rails_root} in place of :rails_root.

Whats the deal????

SOLVED: the :url and :path need to point at a file, not a directory. So I had to have something like

class Cvit < ActiveRecord::Base
  has_attached_file :fileup,
    :url => "/data/01_fasta/:basename.:extension",
    :path => ":rails_root/public/data/01_fasta/:basename.:extension"
end 

回答1:


 has_attached_file :doc, :path => ":rails_root/public/system/attachments/:id/:filename"


def filename
"/system/attachments/#{self.id}/#{self.doc_file_name}"
end

works for me




回答2:


the :url and :path need to point at a file, not a directory. So I had to have something like

class Cvit < ActiveRecord::Base
  has_attached_file :fileup,
    :url => "/data/01_fasta/:basename.:extension",
    :path => ":rails_root/public/data/01_fasta/:basename.:extension"
end 


来源:https://stackoverflow.com/questions/6611624/rails-uploading-files-with-paperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!