使用carrierwave和rmagick上传和调整图像大小时,堆栈级别太深

时间:2014-06-03 06:17:06

标签: ruby-on-rails carrierwave rmagick

当我在ImageUploader类中评论此行“处理:resize_to_fit => [200,300] ”时,代码工作正常,但我想在上传时调整图片大小。

请帮忙。

日志:

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)  

这是我的 ImageUploader 类:

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

这是产品型号:

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

1 个答案:

答案 0 :(得分:9)

我有类似的问题。要修复它,在你的Gemfile中执行以下操作:

 gem 'rmagick', require: false

而不是:

 gem 'rmagick'

重新启动服务器,然后重试。