我目前正在尝试编写类似于互联网上其他类似的自定义图像裁剪系统,用户可以选择裁剪区域,然后相应地裁剪图像.该应用程序位于Rails中,我们将Paperclip与Amazon S3一起使用来存储文件.虽然让RMagick从S3适当裁剪文件,但我遇到了很多麻烦.这是当前代码(不起作用):
if params[:width].to_i > 0 and params[:height].to_i > 0 then
photo = Photo.find(params[:id])
image_data = Net::HTTP.get_response(URI.parse(photo.photo.url(:big))).body
orig_img = Magick::ImageList.new
orig_img.from_blob(image_data)
args = [params[:x1].to_i, params[:y1].to_i, params[:width].to_i, params[:height].to_i]
orig_img.crop!(*args)
photo.update_attributes({:photo => orig_img.to_blob})
photo.photo.reprocess!
photo.save
end
Run Code Online (Sandbox Code Playgroud)
主要问题是裁剪后的图像没有通过回形针上传回S3,因此没有正确裁剪.之前有没有人用回形针尝试这样的东西?这可能是不可能的,但任何帮助将不胜感激.
除了动态文件大小限制之外,还有validates_attachment_size吗?这是一个例子:
class Document < ActiveRecord::Base
belongs_to :folder
has_attached_file :document
validates_attachment_size :document, :less_than => get_current_file_size_limit
private
def get_current_file_size_limit
10.megabytes # This will dynamically change
end
end
Run Code Online (Sandbox Code Playgroud)
我试过这个,但我一直收到错误说"未知方法".Lambdas和Procs也不起作用.有没有人试过这个?谢谢
gem"formtastic","〜> 2.1.1"gem"activeadmin","〜> 0.4.2"gem"paperclip"
照片的字段不会显示在活动管理员表单app/views/admin/products/_form.html.erb中,但app/views/products/_form.html.erb中的相同表单在产品的视图中正常工作
ActiveAdmin.register Product do
form :partial => "form"
end
<%= semantic_form_for [:admin , @product ], :html => { :multipart => true } do |f| %>
<%= f.semantic_errors :name , :price , :description, :category_id %>
<%= f.inputs :new_product do%>
<%= f.input :name %>
<%= f.input :price %>
<%= f.input :description %>
<%= f.input :category_id , :as => :select , :collection => Hash[Category.all.map{|c| [c.name, c.id]}] %>
<% end %>
<%= f.inputs "Product images" do %> …Run Code Online (Sandbox Code Playgroud) 我无法再在我的Rails应用程序中使用paperclip url呈现页面.我最近更新了我的宝石包.我正在使用Rails 3.2.8和Paperclip 3.1.4.我之前使用的是Paperclip 2.7.0.
我视图中失败的链接是:
ad.image.url(:medium)
Run Code Online (Sandbox Code Playgroud)
我的广告模型包含以下声明:
Paperclip.interpolates :ad_subdomain do |attachment, style|
attachment.instance.brand.subdomain
end
has_attached_file :image,
:default_url => '/images/blank.gif',
:styles => { :medium => ["290x230>","jpg"],
:thumb => ["100x100>","jpg"] },
:storage => :file,
:path => "/mcp/ads/:style/:ad_subdomain/:basename.:extension"
Run Code Online (Sandbox Code Playgroud)
抛出的错误是:
TypeError: wrong argument type Class (expected Module)
from /Users/me/.rvm/gems/ruby-1.9.3-p0@mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `extend'
from /Users/me/.rvm/gems/ruby-1.9.3-p0@mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `initialize_storage'
from /Users/me/.rvm/gems/ruby-1.9.3-p0@mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:80:in `initialize'
from /Users/me/.rvm/gems/ruby-1.9.3-p0@mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `new'
from /Users/me/.rvm/gems/ruby-1.9.3-p0@mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `attachment_for'
from /Users/me/.rvm/gems/ruby-1.9.3-p0@mcp5/gems/paperclip-3.1.4/lib/paperclip.rb:191:in `block in has_attached_file'
Run Code Online (Sandbox Code Playgroud) 我将rails应用程序升级rails 2.3.14为rails 3.2.6.在我的模型中,我有以下方法从我的视图中调用以进行图像编辑.
def logo_geometry(style = :original)
@geometry ||= {}
@geometry[style] ||= Paperclip::Geometry.from_file(logo.to_file(style)) # works with s3
end
Run Code Online (Sandbox Code Playgroud)
当调用此方法后发生错误.
undefined method `to_file' for #<Paperclip::Attachment:0xd9d06e0>
Run Code Online (Sandbox Code Playgroud)
任何实现to_file方法功能的建议??
我正在研究铁轨上的红宝石.我正在尝试做文件附件(图像/音频/视频).
所以我有一个常见的方法
byteArray = StringIO.new(File.open("path").read)
Run Code Online (Sandbox Code Playgroud)
是否可以找到byteArray的内容类型来检查上传的文件是否是ruby中的image/audio/video/pdf.
In rails 4.0.2, I am using paperclip gem to upload files. But it is not supporting .doc file. Below the file upload field, it is showing an error message as "has an extension that does not match its contents"
In model, the validation for checking the content type is given below :
validates_attachment_content_type :document, :content_type => ['application/txt', 'text/plain',
'application/pdf', 'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.oasis.opendocument.text',
'application/x-vnd.oasis.opendocument.text',
'application/rtf', 'application/x-rtf', 'text/rtf',
'text/richtext', 'application/doc', 'application/docx', 'application/x-soffice', 'application/octet-stream']
Run Code Online (Sandbox Code Playgroud)
Gems which is used right now
rails (4.0.2, 4.0.0, …Run Code Online (Sandbox Code Playgroud) 我应该为我的项目选择其中一个宝石.哪个更好?Paperclip是否更灵活?谢谢!)
我发送的Json响应是这样的
"ad": {
"id": 3,
"title": "dgdfg",
"description": "kjlj",
"video_file_name": "SampleVideo_1080x720_1mb.mp4",
"thumbnail_file_name": "images.jpeg",
"campaign_id": null,
"duration": null
},
"video_url": "/system/ads/videos/000/000/003/original/SampleVideo_1080x720_1mb.mp4?1448019186"
Run Code Online (Sandbox Code Playgroud)
我希望video_url也与广告对象合并.
我现在发送回复的方式是
render json: {:success=>true, :message=>"Ad detail",:ad=>@ad, :video_url => @ad.video.url}, :status=>200
Run Code Online (Sandbox Code Playgroud)
我如何将其与广告对象合并?
我想发送它
"ad": {
"id": 3,
"title": "dgdfg",
"description": "kjlj",
"video_file_name": "SampleVideo_1080x720_1mb.mp4",
"thumbnail_file_name": "images.jpeg",
"campaign_id": null,
"duration": null,
"video_url": "/system/ads/videos/000/000/003/original/SampleVideo_1080x720_1mb.mp4?1448019186"
}
Run Code Online (Sandbox Code Playgroud)
我的@ad目标是
#<Ad:0x007efc20495f98
id: 3,
title: "dgdfg",
description: "kjlj",
video_file_name: "SampleVideo_1080x720_1mb.mp4",
video_content_type: "video/mp4",
video_file_size: 1055736,
video_updated_at: Fri, 20 Nov 2015 11:33:06 UTC +00:00,
thumbnail_file_name: "images.jpeg",
thumbnail_content_type: "image/jpeg", …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Rails应用程序从使用Paperclip转换为ActiveStorage,并且遇到迁移指南中提供的ConvertToActiveStorage迁移脚本的问题。 https://github.com/thoughtbot/paperclip/blob/master/MIGRATING.md#copy-the-database-data-over
以下是我尝试运行Paperclip到ActiveStorage迁移时遇到的错误。我不确定是什么问题ActiveRecord::Base.connection.execute_prepared
Paperclip当前保存用户上载文件的路径是,迁移后/web/non-public/system/articles/documents/000/000/ActiveStorage应该将其保存到新位置/web/storage。
== 20190123165105 ConvertToActiveStorage: migrating ===========================
-- transaction()
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `execute_prepared' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x007fd7e9211128>
Did you mean? exec_delete
/web/db/migrate/20190123165105_convert_to_active_storage.rb:45:in `block (4 levels) in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:40:in `each'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:40:in `block (3 levels) in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:39:in `each'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:39:in `block (2 levels) in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:28:in `each'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:28:in `block in up'
/web/db/migrate/20190123165105_convert_to_active_storage.rb:27:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
NoMethodError: undefined method `execute_prepared' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x007fd7e9211128> …Run Code Online (Sandbox Code Playgroud) paperclip ×10
ruby ×5
validation ×2
activeadmin ×1
amazon-s3 ×1
bytearray ×1
carrierwave ×1
file ×1
formtastic ×1
json ×1
mime-types ×1
rails-api ×1
rubygems ×1
stringio ×1
thoughtbot ×1