2

When i am commenting this line "process :resize_to_fit => [200, 300]" in ImageUploader class then code is working fine but i want to resize the image when uploading.

please help.

Log:

Started PATCH "/products/980190968" for 127.0.0.1 at 2014-06-03 11:35:08 +0530
Processing by ProductsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"D//KM/EExMDwT7WTMSQpsWnEDgAmpzGz7I7HembUC3s=", "product"=>{"title"=>"With No Image", "description"=>"asdasdasdasdasdasdasdsadasdasdasda", "image_url"=>#<ActionDispatch::Http::UploadedFile:0x2e936b0 @tempfile=#<Tempfile:C:/Users/ahmad/AppData/Local/Temp/RackMultipart20140603-1836-1098spu>, @original_filename="cs.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[image_url]\"; filename=\"cs.jpg\"\r\nContent-Type: image/jpeg\r\n">, "price"=>"39.90"}, "commit"=>"Update Product", "id"=>"980190968"}
  User Load (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 4 LIMIT 1
  Product Load (1.0ms)  SELECT  "products".* FROM "products"  WHERE "products"."id" = $1 LIMIT 1  [["id", 980190968]]
  Cart Load (1.0ms)  SELECT  "carts".* FROM "carts"  WHERE "carts"."id" = $1 LIMIT 1  [["id", 76]]
   (0.0ms)  BEGIN
   (1.0ms)  ROLLBACK
Completed 500 Internal Server Error in 108ms

SystemStackError (stack level too deep):
  actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:79


  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (118.0ms)  

Here is my ImageUploader class:

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
   include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def default_url
     "/images/defaults/notAvailable.jpg"
  end
  
  # Process files as they are uploaded:
  #process :resize_to_fit => [200, 300]

  # For images you might use something like this:
   def extension_white_list
     %w(jpg jpeg gif png)
   end

end

And this is the Product Model:

class Product < ActiveRecord::Base
  has_many :line_items
  has_many :orders, through: :line_items
  #has_and_belongs_to_many :categories
  before_destroy :ensure_not_referenced_by_any_line_item

  validates :title, :description, presence: true
  validates :price, numericality: {greater_than_or_equal_to: 0.01}

  validates :title, uniqueness: true
  validates :image_url, allow_blank: true, format: {
      with: %r{\.(gif|jpg|png)\Z}i,
      message: 'must be a URL for GIF, JPG or PNG image.'
  }
  validates :title, length: {minimum: 10}

  mount_uploader :image_url, ImageUploader

  def self.latest
    Product.order(:updated_at).last
  end

  private

  # ensure that there are no line items referencing this product
  def ensure_not_referenced_by_any_line_item
    if line_items.empty?
      return true
    else
      errors.add(:base, 'Line Items present')
      return false
    end
  end
end
Community
  • 1
  • 1
A H K
  • 1,758
  • 17
  • 29
  • 2
    `Stack Level Too Deep` basically means you've got an infinite loop in your code. Without seeing your `model`, I can only speculate, but I would say the problem is your `processor` will either be self-referencing, or calling a method in an infinite loop – Richard Peck Jun 03 '14 at 06:48
  • 1
    @RichPeck thanks for your comment, i have updated my question with model and Uploader class. – A H K Jun 03 '14 at 07:03
  • Actually Problem is here if you see the image_url value it's something `image_url"=>#, @original_filename="cs.jpg"` Why it's coming like this i don't know. – A H K Jun 03 '14 at 07:16
  • It will be caused by the *processing* part of the image upload procedure - which is why it will be on the `temp` file – Richard Peck Jun 03 '14 at 08:11

1 Answers1

9

I had a similar problem. To fix it, in your Gemfile do this:

 gem 'rmagick', require: false

instead of this:

 gem 'rmagick'

Restart your server and try again.

cristian
  • 8,676
  • 3
  • 38
  • 44