Sinatra,发送一个空页

时间:2013-03-06 21:59:03

标签: ruby sinatra

这是路由块中我的代码的尾部。发生的事情是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模板在其他路由中使用时可以正常工作。有什么想法正在发生什么?

1 个答案:

答案 0 :(得分:1)

正文将是路径块中评估的最后一件事。最后一件事是:

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

File.delete的文档告诉我们:

  

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

所以我猜测target不存在或者是nil,这会使该语句评估为nil。

如果要将主体设置为比结束更早,请使用body helper

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?}