Tag: 回形针

如何使用Paperclip和Image Picker on Rails 4上的options_for_select保存图像?

我正在尝试通过一个表单来保存图像,该表单填充了一个带有多个选项的“选择”字段。 当用户单击选定图像时,选择表单使用图像选择器更改选项。 提交表单后,应使用Paperclip gem保存所选图像。 我想我只需要将所选选项中的图像URL指向一个将在Paperclip中打开它的方法,但我不太清楚如何做到这一点。 我是Rails的新手,谦虚地欣赏任何能指引我正确方向的东西! 这就是我所拥有的: _form.html.erb v ] } )) %> 如果我使用“:medium”,就像我通常在设置表单输入时所做的那样,我得到一个错误,上面写着“ActiveRecord :: AssociationTypeMismatch at / stories Medium(#70334728193840)”,得到字符串(#70334669736760)“,所以我尝试使用“:medium_id”,它显示所选选项的值而不是url。 使用“:medium_id”允许其余的表单字段正确保存而不会出现错误,但只保存图像的选项值,而不是图像本身。 story.rb class Story < ActiveRecord::Base belongs_to :medium accepts_nested_attributes_for :medium stories_controller.rb class StoriesController [], medium_attributes: [:medium, :image], ) medium.rb require “open-uri” class Medium < ActiveRecord::Base has_one :story before_save :image_from_select def image_from_select(url) self.image = open(url) end […]

升级1.8.7到1.9.3,回形针s3上传不起作用

当我从Ruby 1.8.7迁移到1.9.3时,面临Paperclip照片上传的一个问题。 我们配置了Paperclip,s3作为商店。 当我使用1.8.7运行rails服务器时它工作正常,但它没有上传1.9.3的文件(没有任何错误)。 请查看配置和日志。 回形针配置: has_attached_file :pic, :styles => { :thumb => “100×100#”, :one => “118×100#”, :two => “222×149#”, :three => “460×345#”, :popup => “480×360#” }, :storage => :s3, :s3_credentials => Settings.amazon_s3.to_hash, :path => “:attachment/:id/:style/:filename”, :bucket => Settings.amazon_s3.my_bucket 上传图片时记录 [paperclip] Duplicate URL for photo with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in Photo […]

从3.5升级到Paperclip 4.1时没有validates_attachment_file_name

我们的代码看起来像磨纸夹的运行: has_merchants_attached_file :pdf, storage: :s3, s3_credentials: Mbc::DataStore.s3_credentials, s3_permissions: :private, path: “:identifier_template.pdf”, bucket: Mbc::DataStore.forms_and_templates_bucket_name validates_attachment_file_name :pdf, :matches => [/pdf\Z/] 这会产生错误: undefined method `validates_attachment_file_name’ for # 有趣的是,当我们降级到3.5时,我们遇到了同样的问题。 生成它的控制器是: def index @fidelity_templates = FidelityTemplate.order(“identifier asc”).all end 另外: def has_merchants_attached_file(attribute, options={}) if Rails.env.test? || Rails.env.development? has_attached_file attribute, path: “paperclip_attachments/#{options[:path]}” else has_attached_file attribute, options end end 有什么可能导致这种情况的想法?

从rails64上的base64编码图像ruby中检索文件名和内容类型

我正在尝试检索我以base64编码格式接收的图像的内容类型和文件名。 这是使用base64编码图像执行POST请求的代码 require ‘net/http’ require “rubygems” require ‘active_support’ url = URI.parse(‘http://localhost:3000/’) image = ActiveSupport::Base64.encode64(open(“public/images/rails.png”).to_a.join) post_params = {‘image’ => image } Net::HTTP.post_form(url, post_params) 在控制器中,我需要获取此图像的内容类型和文件名。 所以首先我要解码它 image = ActiveSupport::Base64.decode64(params[:image]) image_data = StringIO.new(image) 然后我被卡住了! 我基本上想用paperclip保存这个图像。 需要一些认真的帮助! 更新:我无法发送内容类型和文件名的参数。 我只是模仿发送它的客户端(我无法控制添加额外的参数)

Rails – paperclip – 多张照片上传不保存

我正在尝试在rails中创建一个创建产品页面。 这包括添加多个图像和文本字段。 我有一个产品型号和一个照片。 我正在使用paperclip gem进行照片上传。 但是当我查看产品页面时,我没有得到任何图片。 照片未保存到数据库。 PS我使用HAML。 应用程序/视图/产品/ show.html.haml %b Name = @product.name %br %b Description = @product.description %br – @product.photos.each do |photo| = image_tag photo.image.url 应用程序/控制器/ products_controller class ProductsController “Sale created!” else render “new”, :notice => “Somehting went wrong!” end end def show @product = Product.find(params[:id]) end 应用程序/模型/摄 class Photo { :thumb=> “100×100#”, […]

Rails Paperclip:有没有选项将上传的文件保存在公共文件夹之外?

我正在使用paperclip gem进行上传。 我想将上传的文件/图像保存在公共文件夹之外。 如果您有任何想法请分享。

我的ActiveRecord模型的未定义方法“has_attached_file”

我完全按照https://github.com/thoughtbot/paperclip上的文档在我的应用程序中安装工具回形针以进行图像上传。 我目前正在使用gem’paperclip’,’〜> 5.0.0.beta1’。 完成迁移后,四列已正确添加到我的架构中: t.string “picture_file_name” t.string “picture_content_type” t.integer “picture_file_size” t.datetime “picture_updated_at” 因此,我的回形针应该正确安装。 但是,当我继续将以下两行添加到我的模型类中时: has_attached_file :picture, styles: { medium: “300*300>”, thumb: “100*100” }, default_url: “/images/start_project3.jpg” validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/ 一切都破了。 我尝试在rails控制台中创建,搜索或与模型类相关的任何内容,它对我大吼大叫,出现以下错误: NoMethodError: undefined method `has_attached_file’ for # 我已经尝试了多个版本的回形针,从早期版本4.3.0到最新版本的回形针,但问题仍然存在。 我还在更改和迁移之间重新启动了我的服务器,但这并没有解决问题。 这是我执行的迁移: class AddAttachmentPictureToProjects < ActiveRecord::Migration def self.up change_table :projects do |t| t.attachment :picture end end def […]

用葡萄和回形针上传文件

我正在研究REST API, 尝试上传用户的图片 : 葡萄微框架 回形针gem,但它不起作用,显示此错误 rails版本是3.2.8 No handler found for #<Hashie::Mash filename="user.png" head="Content-Disposition: form-data; name=\"picture\"; filename=\"user.png\"\r\nContent-Type: image/png\r\n" name="picture" tempfile=# type=”image/png”> 我尝试使用控制器测试回形针,但是当我尝试通过葡萄api上传它不起作用我的post标题是multipart / form-data 我的上传代码就是这个 user = User.find(20) user.picture = params[:picture] user.save! 因此,如果无法通过葡萄上传文件,还有其他方法可以通过REST api上传文件吗?

Paperclip如何更改basename(文件名)?

我想更改照片的基名(filename): 在我的模型中,我有: attr_accessor :image_url, :basename has_attached_file :image, :styles => { :original => [“300×250>”, :png], :small => [“165×138>”, :png] }, :url => “/images/lille/:style/:id/:basename.:extension”, :path => “:rails_root/public/images/lille/:style/:id/:basename.:extension” before_save :basename private def basename self.basename = “HALLLO” end 但文件名根本没有改变。

使用Paperclip圆角

如何使用Paperclip创建圆角? 我发现这个解决方案使用convert_options通过回形针创建圆角 ,但它不适用于Rails 3和Paperclip 2.4.5。 仅当我使用ImageMagick alpha参数而不是阈值参数时,生成的convert命令才有效: convert example.jpg \ \( +clone -alpha extract \ -draw ‘fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0’ \ \( +clone -flip \) -compose Multiply -composite \ \( +clone -flop \) -compose Multiply -composite \ \) -alpha off -compose CopyOpacity -composite rounded_corners.png 对应于 has_attached_file :avatar, :styles […]