【发布时间】:2014-03-02 13:23:02
【问题描述】:
我正在使用 Rails 4,并尝试上传图像,然后在处理后存储它。
我只使用一个视图,我希望用户上传图像,我处理图像,将其存储在数据库中,然后使用新处理的图像重新加载页面。
我的模型(user.rb)
class User < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
上传器 (image_uploader.rb)
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :changeImage
def changeImage
manipulate! do |source|
source = source.sepiatone
end
end
控制器 (app_main_controller.rb)
require 'Rmagick'
class AppMainController < ApplicationController
def index
# page reload handling when the file uploads
if(params.has_key?("file-input"))
@u= User.new
@u.image = params["file-input"]
if(@u.save)
render js: "alert('SAVED')"
else
render js: "alert('Error while saving image! Try Again!')"
end
# initial page load
else
end
end
end
我使用image_tag @u.image_url在我的视图(即索引)中访问图像上传器
每次在 image_uploader.rb 中添加任何类型的处理时,我都会不断得到“堆栈级别太深”。如果不加处理,图片上传正常。
有什么想法吗?
【问题讨论】:
-
“堆栈级别太深”通常意味着您正在递归调用函数而没有任何结束递归条件。因此,从您的代码来看,我的猜测是
source = source.sepiatone以某种方式再次触发了 process 方法,从而导致无限递归。 -
.sepiatone 是 RMagick gem 中的一种方法,所以我认为它存在一些问题。
标签: ruby-on-rails ruby ruby-on-rails-4 imagemagick rmagick