【发布时间】:2016-02-03 03:32:15
【问题描述】:
我正在尝试从 Jcrop 获取用户隐藏字段参数并在将图像放入数据库之前对其进行裁剪。我找到了一些教程和问题,但没有一个能解决我的确切问题。
这是最接近的。看起来海报试图使用选择中的单个参数。我有 4 个整数。我认为这无关紧要,所以我认为这是“has_attached_file”部分:
Supplying a variable string to model for Paperclip Imagemagik resizing
结合这个:
https://github.com/thoughtbot/paperclip#dynamic-configuration
这就是我所拥有的,
型号:
class Muse < ActiveRecord::Base
attr_accessor :w, :h, :x, :y
def get_coor
#turn parameters into class variables
@w = self.w
@h = self.h
@x = self.x
@y = self.y
#build string for options
cropper = "-crop #{@w.to_i}x#{@h.to_i}+#{@x.to_i}+#{@y.to_i} -resize 200x200"
#return string of the convert options I want
@get_coor = cropper
end
belongs_to :user
default_scope -> { order(created_at: :desc) }
has_attached_file :muse_img, styles: lambda { |attachment| { original: (attachment.instance.get_coor) } }
validates_attachment :muse_img, presence: true,
content_type: { content_type: ["image/jpeg"] },
size: { in: 0..500.kilobytes }
end
我没有收到错误。但它的行为就像 get_coor 方法没有调用任何东西。
在此之前,我直接在 convert_options 上尝试了字符串插值,但对于所有坐标,它始终显示为 nil。我是 Rails 和 Ruby 的新手,所以我对 lambdas 还是有些模糊。希望这只是一个语法错误。感谢您的帮助!
【问题讨论】:
标签: ruby-on-rails ruby lambda paperclip jcrop