【问题标题】:Paperclip Rails 4 Reprocess attr_accessorPaperclip Rails 4 重新处理 attr_accessor
【发布时间】:2013-09-19 00:05:55
【问题描述】:

我的 rails 4 应用程序在使用回形针重新处理方法时遇到了一些问题。我有一个自定义 Cropper 模块:

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.sub(/ -crop \S+/, '')
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ratio = target.avatar_geometry(:original).width  / target.avatar_geometry(:large).width
        ["-crop", "#{(target.crop_w.to_i*ratio).round}x#{(target.crop_h.to_i*ratio).round}+#{(target.crop_x.to_i*ratio).round}+#{(target.crop_y.to_i*ratio).round}"]
      end
    end
  end
end

我有一些用于crop_x、crop_y、crop_w、crop_h的attr_accessors。

我遇到了一个问题,即当它到达cropper 类时,crop_x、y、w 和 h 可用。即使在控制器中(在更新方法期间)这些元素不是 nil,这些元素也始终为 nil。

我相信这与 attr_accessors 有关,所以我正在寻找一些关于如何处理这个问题的建议。

【问题讨论】:

    标签: ruby-on-rails ruby paperclip


    【解决方案1】:

    在处理更新的控制器中,您是否包含 :crop_x、:crop_y、:crop_w、:crop_h 作为允许的参数?

    你应该有这样的东西:

    params.require(:model).permit(:attribute1, :attribute2, :crop_x, :crop_y, :crop_w, :crop_h)
    

    【讨论】:

    • 是的。我认为这可以通过找工作并让工人来解决。因为如果我更改代码,代码将在尚未加载的图像上失败。如果我添加一个工作,那么这些数据会保存在数据库中吗?也许这样会更好?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多