Paperclip是Rails的一个很棒的上传插件.将上传存储在本地文件系统或Amazon S3上似乎运行良好.我只假设本地主机上存储文件,但此应用程序需要使用S3,因为它将托管在Heroku上.
如何通过单个压缩下载从S3获取所有上传/附件?
从本地文件系统获取zip 文件似乎很简单.它让S3中的文件让我感到困惑.我认为这可能与rubyzip处理URL引用的文件的方式有关.我尝试了各种方法,但似乎无法避免错误.
format.zip {
registrations_with_attachments = Registration.find_by_sql('SELECT * FROM registrations WHERE abstract_file_name NOT LIKE ""')
headers['Cache-Control'] = 'no-cache'
tmp_filename = "#{RAILS_ROOT}/tmp/tmp_zip_" <<
Time.now.to_f.to_s <<
".zip"
# rubyzip gem version 0.9.1
# rdoc http://rubyzip.sourceforge.net/
Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE) do |zip|
#get all of the attachments
# attempt to get files stored on S3
# FAIL
registrations_with_attachments.each { |e| zip.add("abstracts/#{e.abstract.original_filename}", e.abstract.url(:original, false)) }
# => No such file or directory - http://s3.amazonaws.com/bucket/original/abstract.txt
# Should note that these files …Run Code Online (Sandbox Code Playgroud) 我有一个rake文件,它通过HTTP读取内容,我想使用Paperclip将加载的内容存储在Amazon S3上.当我提供本地文件时,它工作正常,但我想将内容设置为字符串并手动设置内容类型.
以下不起作用.未发出错误,数据库条目已更新,但在S3中未创建任何文件:
p.attachment = "Test"
p.attachment_file_name = "test.txt"
p.attachment_content_type = "text/plain"
p.attachment_file_size = "Test".size
p.attachment_updated_at = Time.now
p.save
Run Code Online (Sandbox Code Playgroud)
我想我可以用我的内容写一个临时文件,但这将是一个非常低效的解决方案.
使用Amazon S3时如何使用回形针创建路径?
我的目录上的目录是:
/image/:id/:filename
Run Code Online (Sandbox Code Playgroud)
我的模特:
has_attached_file :image,
:storage => :s3,
:bucket => 'mybucket',
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
Run Code Online (Sandbox Code Playgroud) 我在保存之前使用此方法重命名图像:
def rename_avatar
self.avatar.instance_write :file_name, Time.now.to_i.to_s
end
before_post_process :rename_avatar
Run Code Online (Sandbox Code Playgroud)
图像被当前时间重命名,但没有添加文件类型,而是1334487964.jpg仅保存1334487964.
那里我想念的是什么?我想:file_name只包含文件名 - 没有文件类型
有没有办法阻止验证消息出现两次Paperclip上传验证?
这是我的模型:
has_attached_file :photo, :styles => { :thumb => "215x165" }, :default_url => "/images/:style/missing.png"
validates_attachment :photo, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..0.5.megabytes }
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
<% if @product.errors.any? %>
<p>The following errors were found:</p>
<ul>
<% @product.errors.full_messages.each do |message| %>
<li>- <%= message %></li>
<% end %>
</ul>
<% end %>
Run Code Online (Sandbox Code Playgroud)
如果我上传无效文件,我会收到以下错误消息:
有没有办法让其中一个出现?我尝试过向模型添加消息:但那么这也只是出现了两次!
谢谢!
作为参考,我一直在关注本教程:https://devcenter.heroku.com/articles/paperclip-s3除了我现在在localhost测试中,所以我安装了Figaro gem并将我的S3信息放在application.yml中.
使用Rails v4,Cocaine v0.5.3和Paperclip v4.1.0(不确定是否需要提及任何其他gem版本号).
我有一个名为SubmissionDetails的模型,其new.html.erb中我试图将jpg上传到名为attachment的列.以下是相关的型号代码:
has_attached_file :attachment, styles: {
thumb: '200x200>',
large: '800x800>'
}
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
Run Code Online (Sandbox Code Playgroud)
当我尝试上传jpg时,它返回到表单,并显示以下错误消息:
1 error prohibited this submission_detail from being saved:
Attachment translation missing:
en.activerecord.errors.models.submission_detail.attributes.attachment.spoofed_media_type
Run Code Online (Sandbox Code Playgroud)
我理解了一些错误,我的en.yml文件中缺少显示此错误消息的文本,但那个欺骗媒体类型部分呢?
这显示在我的服务器控制台中,不确定这是否相关:
[paperclip] Content Type Spoof: Filename header.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
(0.0ms) rollback transaction
Run Code Online (Sandbox Code Playgroud) 我有一个带回形针的Rails 3应用程序.我想通过回形针保存宽度/高度为LTE 50x50的图像.
这可能吗?
看起来像Paperclip不尊重ActiveRecord脏模型.如何检测after_save回调中的更改.
class User
has_attachment :avatar
after_save :do_something
def do_something
if name_changed?
#
end
# How to determine avatar was changed?
#if avatar_changed?
# #
#end
end
end
Run Code Online (Sandbox Code Playgroud)
注意
我知道我可以before_save通过avatar.dirty?调用检测回调的变化,但dirty保存后该标志设置为false.
我可以添加处理器,但是我需要在保存模型数据后执行我的操作.
然后,我想使用Paperclip为每个清单提供照片.我将相应的代码添加到列表show.html.erb,listing.rb模型,listings_controller.rb和_form.html.erb部分.
当我尝试上传列表的图像时,我收到此错误:
Paperclip::Error in ListingsController#update
Listing model missing required attr_accessor for 'avatar_file_name'
Run Code Online (Sandbox Code Playgroud)
listing_controller的第44行:
def update
respond_to do |format|
if @listing.update(listing_params)
format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
format.json { head :no_content }
else
Run Code Online (Sandbox Code Playgroud)
要尝试的一些事情:即向listing.rb模型添加一些代码,以使:avatar的可接受图像更加健壮.以下是几个stackoverflow帖子中提到的添加到listing.rb模型的内容:
validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png)
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我附加图像时,我仍然会遇到相同的错误.当我没有附加图像时,我的默认图像被正确加载并正确创建列表.
我的清单模型:
class Listing < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "150x", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png)
end
Run Code Online (Sandbox Code Playgroud)
我的_form.html.erb部分:
<%= form_for @listing, :html => { …Run Code Online (Sandbox Code Playgroud) 当我试图提交表单时,发生以下错误:Validation failed: Images imageable must exist并呈现相同的new.html.erb视图.
如果我评论了file field在new.html.erb.产品正在成功创建.
ProductsController的:
def new
@product = Product.new
end
def create
@product = Product.create!(product_params)
if @product.save
redirect_to products_path, notice: "Product Created Successfully"
else
render "new"
end
end
def product_params
params.require(:product).permit(:name, :quantity, :price, images_attributes: [:id, :photo, :_destroy])
end
Run Code Online (Sandbox Code Playgroud)
new.html.erb:
<%= nested_form_for @product, html: { multipart: true } do |f|%>
<h2>New</h2>
<P> <%= f.label :name %> <%= f.text_field :name %> </P>
<P> <%= f.label :quantity %> <%= f.text_field …Run Code Online (Sandbox Code Playgroud)