4

I am using Rails 4, and trying to upload an image and then store it after processing it.

I am using just one view, where I want the user to upload the image, I process the image, store it in db, and then reload the page with the new processed image.

My model (user.rb)

class User < ActiveRecord::Base
    mount_uploader :image, ImageUploader
end

Uploader (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

Controller (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

I access the image-uploader in my view (i.e. index) with image_tag @u.image_url

I keep on getting "stack level too deep" everytime I add any kind of processing in image_uploader.rb. If no processing is added, the image gets uploaded fine.

Any ideas, anyone?

utsavanand
  • 329
  • 1
  • 3
  • 15
  • "stack level too deep" usually means you are calling a function recursively without any end recursion condition. So from your code, my guess is `source = source.sepiatone` is somehow triggering process method again which leads into infinite recursion. – Zeeshan Mar 03 '14 at 02:06
  • .sepiatone is a method from the RMagick gem, so I suppose there is some problem it. – utsavanand Mar 03 '14 at 08:40

1 Answers1

1

I am experiencing the same problem. It looks like an issue with the rmagick gem.

EDIT: The problem is with case. Try this instead:

gem 'rmagick', :require => 'RMagick'

See also this stackoverflow answer and this issue or this issue on github.

I switched to mini_magick to fix it. (Not ideal, but it's working for me now.)

Uploader class (comment out RMagick):

include CarrierWave::MiniMagick

Gemfile:

gem 'mini_magick'
Community
  • 1
  • 1
Adam Pearlman
  • 971
  • 1
  • 9
  • 16
  • Actually, I had tried with MiniMagick as well. The resizing works fine. But it starts giving me "NoMethodErrors" for .sepiatone and which ever other methods I use. – utsavanand Mar 03 '14 at 08:37