Ruby on rails Rails 4-使用自定义曲别针附件处理器解析参数的顺序
我有一个带有回形针图像附件的内容表,以及一些用户选择的裁剪设置:Ruby on rails Rails 4-使用自定义曲别针附件处理器解析参数的顺序,ruby-on-rails,image,paperclip,Ruby On Rails,Image,Paperclip,我有一个带有回形针图像附件的内容表,以及一些用户选择的裁剪设置: create_table "content", force: true do |t| t.string "title" t.string "image_file_name" t.string "image_content_type" t.integer "image_file_size" t.datetime "image_updated_at" t.integer
create_table "content", force: true do |t|
t.string "title"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "crop_x"
t.integer "crop_y"
t.integer "crop_w"
t.integer "crop_h"
end
为了在服务器端处理用户指定的图像裁剪,我有一个定制的回形针处理器,我的来源是:
是否有一种干净的方法告诉Rails在附件图像之前处理裁剪边界字段?或者是否有一种不同的解决方案基本上实现了这一点?就我而言,我无法找到一种“干净”的方法。我最后做的是在调用附件的更新/保存之前保存
裁剪
值
例如:
def update
if article_params[:image_crop_y].present?
@article.image_crop_y = article_params[:image_crop_y]
@article.image_crop_x = article_params[:image_crop_x]
@article.image_crop_w = article_params[:image_crop_w]
@article.image_crop_h = article_params[:image_crop_h]
@article.save
end
if @article.update(article_params)
...
end
end
就像我说的,这不是最“干净”的方法,但对我来说很有效。很酷,谢谢蒂姆!我想我做了一件更骇人的事情,那就是在DB模式中将裁剪字段放在图像附件字段之前。(Rails按架构顺序处理参数)。因此,代码看起来很干净,但如果Rails实现中发生了变化,那么最终是危险的。。谢谢你的解决方案!
# rails processes the image_xxx params before the crop_xxx params
@content = Content.new(content_params)
def update
if article_params[:image_crop_y].present?
@article.image_crop_y = article_params[:image_crop_y]
@article.image_crop_x = article_params[:image_crop_x]
@article.image_crop_w = article_params[:image_crop_w]
@article.image_crop_h = article_params[:image_crop_h]
@article.save
end
if @article.update(article_params)
...
end
end