0

这是我的代码在路由块中的结尾。发生的事情是 success_loader 从不渲染,而是 Sinatra 发送空响应(页面为空白)

if v.test
  mydata = v.sheetData
end
if v.errors.size == 0
  #success
  haml :success_loader, :layout => :base, 
       :locals => {:pub_path => settings.root, :app_ver => settings.version }
else
  #fail
  haml :failure_loader, :layout => :base, 
       :locals => {:pub_path => settings.root, :app_ver => settings.version, 
       :app_error => v.errors }
end 
File.delete(target) if File.exists?(target)

使用调试器,我显示从对 haml 的调用开始的点开始的每一行

haml :success_loader, :layout => :base, 
     :locals => {:pub_path => settings.root, :app_ver => settings.version }
getter = proc { value }

value = :pub_path

getter = proc { value }

value = :apper_ver

File.delete(target) if File.exists?(target)
@params = @original_params
filter! :after unless env['sinatra.static_file']
res = [res] if Fixnum === res or String === res
if Array === res and Fixnum === res.first
status(res.shift)
body(res.pop)
headers(*res)
invoke { error_block!(response.status) }
unless @response['Content-Type'

一堆更多类似的行。然后从 Thin 开始的几行:

log "!! Rack application returned nil body. Probably you wanted it to be an empty string?" if @response.body.nil?

我在路由之外测试了代码,所以它不会抛出任何异常。当在其他路线中使用时,haml 模板可以正常工作。有什么想法吗?

4

1 回答 1

1

主体将是路由块中评估的最后一个东西。最后一件事是:

File.delete(target) if File.exists?(target)

File.delete的文档告诉我们:

删除命名文件,返回作为参数传递的名称数。

所以我猜那target不存在或曾经是nil,这会使该陈述评估为零。

如果要在结束之前设置正文,请使用正文助手

body = if v.errors.size == 0
  #success
  haml :success_loader, :layout => :base, 
       :locals => {:pub_path => settings.root, :app_ver => settings.version }
else
  #fail
  haml :failure_loader, :layout => :base, 
       :locals => {:pub_path => settings.root, :app_ver => settings.version, 
       :app_error => v.errors }
end 
File.delete(target) if File.exists?(target)

但这可以重构为:

template = v.errors.size == 0 ? :success_loader : :failure_loader
File.delete(target) if File.exists?(target)
haml template, :layout => :base, 
               :locals => {
                 :pub_path => settings.root,
                 :app_ver => settings.version, 
                 :app_error => v.errors
               }.delete_if{|k,v| v.nil?}
于 2013-03-07T14:11:29.647 回答