为 Paperclip Imagemagik 调整大小提供变量字符串模型 [英] Supplying a variable string to model for Paperclip Imagemagik resizing
问题描述
我正在使用回形针上传一些调整大小的图像.其中一种我想以五种方式之一进行裁剪......无论如何,我通过手动更改它们确定了要裁剪的字符串应该是什么样子,但现在我需要使其动态化,以便回形针可以基于裁剪用户想要什么...
I'm using paperclip to upload some images which get resized. One of which I want to be cropped one of five ways... Anyway, I worked out what the strings to crop should all look like, by changing them by hand, but now i need to make that dynamic so that paperclip can crop based upon what the user wants...
问题是我得到了
undefined local variable or method `params' for #<Class:0x00000105b228d8>
我很确定这是因为我正试图按照自己的意愿弯曲铁轨.无论如何,我认为我想要做的事情很清楚......只需将crop_geometry_thumb变量提供给convert_options......我应该把这个逻辑放在哪里,我的模型将能够找到它?
I feel pretty sure that this is because I'm attempting to bend rails to my will. Anyway, I think it's pretty clear what I'm trying to do... Just supply the crop_geometry_thumb variable to convert_options... Where should I actually be putting this logic that my model will be able to find it?
class Asset < ActiveRecord::Base
if params[:crop_geometry] == "bottom"
crop_geometry_thumb = "-crop 200x100+0+100 -scale 100x100"
elsif params[:crop_geometry] == "top"
crop_geometry_thumb = "-crop 200x100+0+0 -scale 100x100"
elsif params[:crop_geometry] == "left"
crop_geometry_thumb = "-crop 100x200+0+100 -scale 100x100"
elsif params[:crop_geometry] == "right"
crop_geometry_thumb = "-crop 100x200+100+0 -scale 100x100"
else
crop_geometry_thumb = "-scale 100x100"
end
belongs_to :piece
has_attached_file :asset, :styles => {
:large => ['700x700', :jpg],
:medium => ['300x300>', :jpg],
:thumb => ["200x200>", :jpg]},
:convert_options => {:thumb => crop_geometry_thumb}, ### supply a string from above... FAIL :(
:path => ":id/:style/:filename",
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:s3_permissions => :private,
:url => ':s3_domain_url'
end
推荐答案
所以当前的问题是请求参数(即 params[:crop_geometry]
)不能被你的模型访问,只能被你的模型访问控制器+视图.
So the immediate problem is that request params (i.e. params[:crop_geometry]
) are not accessible to your model, only to your controller + views.
在某些情况下(尽管这从来都不是一个好主意),您可以通过将参数作为方法的参数传递给模型来绕过此 MVC 规则:
In some cases (though it's never really a good idea), you can get around this MVC rule by passing params to your model as an argument for a method:
class FoosController < ApplicationController
def action
Foo.some_method(params)
end
end
class Foo < ActiveRecord::Base
some_method(params)
puts params[:crop_geometry]
end
end
相反,我建议将该参数信息传递到模型中定义的实例变量中,并将条件逻辑放入自定义 setter 方法中,如下所示:
Instead, I'd recommend passing that param information into an instance variable defined in the model, and putting the conditional logic into a custom setter method, like so:
class Asset < ActiveRecord::Base
attr_reader :crop_geometry
def crop_geometry=(crop_type)
if crop_type == "bottom"
crop_string = "-crop 200x100+0+100 -scale 100x100"
elsif crop_type == "top"
crop_geometry_thumb = "-crop 200x100+0+0 -scale 100x100"
elsif crop_type == "left"
crop_geometry_thumb = "-crop 100x200+0+100 -scale 100x100"
elsif crop_type == "right"
crop_geometry_thumb = "-crop 100x200+100+0 -scale 100x100"
else
crop_geometry_thumb = "-scale 100x100"
end
@crop_geometry = crop_geometry_thumb
end
end
请注意,您必须更改表单,以便为 params[:asset][:crop_geometry] 分配顶部"、底部"或其他任何内容.
Note that you'll have to change your form so that it assigns 'top', 'bottom' or whatever to params[:asset][:crop_geometry].
现在,要动态设置crop_geometry,您需要在has_attached_file 配置中使用lambda——这样每次访问配置时都会对其进行评估,而不仅仅是在最初加载模型时.给你:
Now, to dynamically set the crop_geometry, you'll need to use a lambda in the has_attached_file configuration--that way it'll be evaluated each time the configuration is accessed, not just when the model is initially loaded. Here you go:
has_attached_file :asset, :styles => lambda {|attachment|
:large => ['700x700', :jpg],
:medium => ['300x300>', :jpg],
:thumb => ["200x200>", :jpg]},
:convert_options => {:thumb => attachment.instance.crop_geometry},
:path => ":id/:style/:filename",
...
}
最后一部分来自 https://github.com/thoughtbot/paperclip(寻找动态配置").
Got that last part from https://github.com/thoughtbot/paperclip (look for "Dynamic Configuration").
这篇关于为 Paperclip Imagemagik 调整大小提供变量字符串模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!