从 Carrierwave 上传器内部访问模型

发布于 2024-11-02 07:12:45 字数 422 浏览 1 评论 0原文

我正在尝试为我正在开发的 Web 应用程序实现手动裁剪,到目前为止我已经做到了:

  version :croppedversion do
    process :manualcrop => [model.crop_x, model.crop_y, model.crop_h, model.crop_w]
  end

  process :resize_to_limit => [600, 600]

  def manualcrop(x,y,h,w)
    manipulate! do |img|
      img = img.crop(x,y,h,w)
    end
  end

问题是模型显示为零。从我读过的文档来看,这将是正确的方法。

有什么建议吗?我现在已将裁剪调用移至模型中的回调中,但我真的希望它驻留在载波内

I'm trying to implement a manual crop for a web application I'm working on, and I have this so far:

  version :croppedversion do
    process :manualcrop => [model.crop_x, model.crop_y, model.crop_h, model.crop_w]
  end

  process :resize_to_limit => [600, 600]

  def manualcrop(x,y,h,w)
    manipulate! do |img|
      img = img.crop(x,y,h,w)
    end
  end

The problem is that model is turning up as nil. From the documentation I've read this would be the correct way to go about it.

Any suggestions? I've moved the call to cropping into a callback in the model for now, but would really like for it to reside inside carrierwave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2024-11-09 07:12:45

我遇到了类似的问题。我有

attr_accessor:crop_x,:crop_y,:crop_w,:crop_h

但忘记将它们包含在 attr_accessible 中的作物变量。通过做

attr_accessible:描述、:图像、:crop_x、:crop_y、:crop_w、:crop_h

我能够获取模型作物变量。我不知道这是否可以解决您的问题,因为您描述的是返回 nil 的模型而不是裁剪方法。

I ran into a similar issue. I had

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

but forgot to include them the crop variables in the attr_accessible. By doing

attr_accessible :description, :image, :crop_x, :crop_y, :crop_w, :crop_h

I was able to get the model crop variables. I dont know if this would solve your issue because you're describing the model returning nil rather than the crop methods.

携君以终年 2024-11-09 07:12:45

当我尝试访问模型数据时,我的对象在 CarrierWave 中为零,对此我感到非常挣扎。

对我来说,问题出在我的控制器上。这看起来不太适合您的情况,但我正在通过执行以下操作来运行查询: model.user.foo - 如果我在创建对象后立即将此用户分配给模型,它将出现 nil 。

I struggled very much with my object being nil in CarrierWave when trying to access model data.

For me, the issue was in my controller. This doesn't look like it applies too much to your situation, but I was running a query by doing: model.user.foo - It would come up nil if I was immediately assigning this user to the model after creating the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文