ruby-on-rails - 使用 Carrierwave 和 rmagick 上传图像并调整图像大小时堆栈级别太深

标签 ruby-on-rails carrierwave rmagick

当我在 ImageUploader 类中评论这一行“process :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

最佳答案

我也遇到了类似的问题。要修复它,请在 Gemfile 中执行以下操作:

 gem 'rmagick', require: false

而不是这个:

 gem 'rmagick'

重新启动服务器并重试。

关于ruby-on-rails - 使用 Carrierwave 和 rmagick 上传图像并调整图像大小时堆栈级别太深,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24008308/

相关文章:

ruby-on-rails - 如何在 Windows 7 32 位中为 Ruby on Rails 安装 Rmagick 和 imageMagick

ruby-on-rails - 没有迁移表的 Rails STI 模型

javascript - 格式化 Rails 使用的 javascript 变量

ruby-on-rails - 使用 Carrierwave 重命名上传的文件

ubuntu - Controller 中的 Errno::EACCES#upload rails roo carrierwave on ubuntu

ruby-on-rails - osx 10.8.5 - Ruby on Rails - rails s 上的 rmagick 错误

ruby-on-rails - 如何在 Rails 5 中启用源映射生成

ruby-on-rails - 合并两个ActiveRecord数组并按created_at排序

rename - 如何重命名所有现有的 Carrierwave 上传的文件?

ruby - 当文件大小大于 X 时 Rmagick 压缩而不写入(上传到 S3)