carrierwave在文件上传时抛出“堆栈级别太深”错误

时间:2011-07-27 08:59:43

标签: ruby-on-rails-3 carrierwave

我有一个问题,现在还没有进一步调试......

如果我使用我的html表单上传文件,我会:

SystemStackError (stack level too deep):

跟踪是:

Started POST "/global/accounts/82" for 127.0.0.1 at 2011-07-27 10:28:03 +0200
  Processing by Global::AccountsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tAf/cGPjW+uGgdl6J7t+IZgGsNKkVDLCCWYMFdtQd7g=", "account"=>{"logo_cache"=>"", "shortcut_icon"=>#<ActionDispatch::Http::UploadedFile:0x0000010632daa0 @original_filename="18677_265409985796_708130796_4889342_5500573_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"account[shortcut_icon]\"; filename=\"18677_265409985796_708130796_4889342_5500573_n.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20110727-3683-1yazc7m>>, "shortcut_icon_cache"=>""}, "commit"=>"Einstellungen speichern", "member"=>{"cancel"=>:get}, "id"=>"82"}
  User Load (1.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 261 LIMIT 1
  Account Load (0.8ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 82 LIMIT 1
  SQL (2.0ms)  describe `roles_users`
  Role Load (3.8ms)  SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE `roles`.`name` = 'admin' AND (`roles_users`.user_id = 261 ) LIMIT 1
  Account Load (1.9ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = '***' LIMIT 1
  SQL (0.2ms)  BEGIN
  SQL (0.6ms)  SELECT 1 FROM `accounts` WHERE (LOWER(`accounts`.`subdomain`) = LOWER('***')) AND (`accounts`.id <> 82) LIMIT 1
  AREL (0.5ms)  UPDATE `accounts` SET `shortcut_icon` = 'aadf09e05f4db4124c62bfb9340aa9bd.jpg', `updated_at` = '2011-07-27 08:28:08' WHERE `accounts`.`id` = 82
  Account Load (0.5ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 82 LIMIT 1
  SQL (1.2ms)  ROLLBACK
Completed   in 2831ms

SystemStackError (stack level too deep):

分析跟踪,似乎文件已上传并写入数据库:

 AREL (0.5ms)  UPDATE `accounts` SET `shortcut_icon` = 'aadf09e05f4db4124c62bfb9340aa9bd.jpg', `updated_at` = '2011-07-27 08:28:08' WHERE `accounts`.`id` = 82

但之后会抛出错误......

在rails控制台中测试carrierwave:

ruby-1.9.2-p180> path = "/Users/kalle/Desktop/button.png"
ruby-1.9.2-p180> u = Account.last
ruby-1.9.2-p180> u.logo = File.open(path)
 => #<File:/Users/kalle/Desktop/button.png> 
ruby-1.9.2-p180> u.save!
 => true 

工作正常!

好吧,我有一个文件上传@另一个模型,所以carrierwave安装工作正常(类似的上传器)。

测试html表单没有提交文件,它运行正常!

这样:

  • 表单无文件字段
  • 运营商波文件上传在控制台中运行
  • 不同的型号,类似的载波上传器/配置=&gt;作品!

我该如何进一步调试?

感谢您的帮助!

Rails 3.0.7 / ruby​​-1.9.2-p180 / carrierwave(0.5.3)

编辑: 似乎它只发生在更新操作上。

控制器:

def update
  if current_account.update_attributes(params[:account])
    flash[:notice] = 'Successfully updated account.'
    redirect_to global_settings_path
  else
    render :action => 'edit'
  end
end

1 个答案:

答案 0 :(得分:1)

我有类似的问题。 当我调用photo.url时,它会抛出“堆栈级别来解决深层错误”。

我的PhotoUploader中似乎有以下代码:

  def url
    filename = self.to_s
    content_type = File.mime_type?(filename)    
    ext = {
      'image/png'  => :png,
      'image/jpg'  => :jpg,
      'image/jpeg' => :jpg,
      'image/gif'  => :gif

    }[content_type]
    ext ||= :jpg

    { :id => model.id, :version => version_name, :format => ext }    
  end

这个代码似乎在carrierwave 0.5.8中运行良好。 我更新到0.6.2,因为它失败了。

问题是我正在调用self.to_s。正如我在carrierwave代码中发现的那样: to_s的实现方式如下:

  def to_s
    url || ''
  end

通过将self.to_s更改为self.file,问题得以解决

  def url
    filename = self.file
    # ... rest of the code ..
  end