当我使用 RespondWith 时,Sinatra 尝试加载 haml 引擎
Sinatra tries to load haml engine when I use RespondWith
我想让相同的 URL 响应 HTML 或 JSON,具体取决于请求的接受 header,所以我正在尝试使用RespondWith extension 来自 Sinatra::Contrib.
每当我向 respond_with()
提供模板名称时,我都会收到错误消息。该错误似乎是在它尝试加载 Haml 模板引擎时发生的。
我只想使用Erb模板引擎,那么如何禁用Haml模板引擎呢?
我看到的错误是,"LoadError - cannot load such file -- haml"。
这是运行 Web 服务器的 app.rb
文件:
require 'rubygems'
require 'sinatra'
require 'sinatra/respond_with'
require 'tilt/erubis'
get '/' do
@name = 'Bob'
json_data = { :name => @name }
respond_with :'index.html', json_data
end
这是模板文件views/index.html.erb
:
<html>
<body>
<h1>Index Page</h1>
<h2><%= @name %></h2>
</body>
</html>
这是我请求 HTML 或 JSON 时收到的错误的完整堆栈跟踪:
2015-12-18 15:57:28 - LoadError - cannot load such file -- haml:
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/haml.rb:2:in `<top (required)>'
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:236:in `block in lazy_load'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:234:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:234:in `lazy_load'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:214:in `lookup'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:152:in `[]'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt.rb:48:in `[]'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:182:in `block in template_for'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:177:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:177:in `template_for'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:139:in `block (2 levels) in respond_with'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt.rb:88:in `fetch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:139:in `block in respond_with'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:115:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:115:in `block in finish'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:114:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:114:in `finish'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:159:in `respond_with'
app.rb:18:in `block in <main>'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1610:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1610:in `block in compile!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:974:in `[]'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:974:in `block (3 levels) in route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:993:in `route_eval'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:974:in `block (2 levels) in route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1014:in `block in process_route'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1012:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1012:in `process_route'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:972:in `block in route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:971:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:971:in `route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1084:in `block in dispatch!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `block in invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1081:in `dispatch!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:906:in `block in call!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `block in invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:906:in `call!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:894:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/logger.rb:15:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/commonlogger.rb:33:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:218:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:211:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/show_exceptions.rb:21:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:181:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:2021:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1486:in `block in call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1795:in `synchronize'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1486:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/thin-1.6.3/lib/thin/connection.rb:86:in `block in pre_process'
/home/don/.rvm/gems/ruby-2.2.2/gems/thin-1.6.3/lib/thin/connection.rb:84:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/thin-1.6.3/lib/thin/connection.rb:84:in `pre_process'
/home/don/.rvm/gems/ruby-2.2.2/gems/eventmachine-1.0.8/lib/eventmachine.rb:1062:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/eventmachine-1.0.8/lib/eventmachine.rb:1062:in `block in spawn_threadpool'
Tilt 模板引擎注册表似乎默认注册了一大堆引擎。如果我只是显式注册我想要的模板引擎,那么它就会停止尝试加载其他所有内容。
require 'rubygems'
require 'sinatra'
require 'sinatra/respond_with'
require 'tilt/erubis'
set :template_engines, {
:css=>[],
:xml=>[],
:js=>[],
:html=>[:erb],
:all=>[:erb],
:json=>[]
}
get '/' do
@name = 'Bob'
json_data = { :name => @name }
respond_with :'index.html', json_data
end
我想让相同的 URL 响应 HTML 或 JSON,具体取决于请求的接受 header,所以我正在尝试使用RespondWith extension 来自 Sinatra::Contrib.
每当我向 respond_with()
提供模板名称时,我都会收到错误消息。该错误似乎是在它尝试加载 Haml 模板引擎时发生的。
我只想使用Erb模板引擎,那么如何禁用Haml模板引擎呢?
我看到的错误是,"LoadError - cannot load such file -- haml"。
这是运行 Web 服务器的 app.rb
文件:
require 'rubygems'
require 'sinatra'
require 'sinatra/respond_with'
require 'tilt/erubis'
get '/' do
@name = 'Bob'
json_data = { :name => @name }
respond_with :'index.html', json_data
end
这是模板文件views/index.html.erb
:
<html>
<body>
<h1>Index Page</h1>
<h2><%= @name %></h2>
</body>
</html>
这是我请求 HTML 或 JSON 时收到的错误的完整堆栈跟踪:
2015-12-18 15:57:28 - LoadError - cannot load such file -- haml:
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/haml.rb:2:in `<top (required)>'
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:236:in `block in lazy_load'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:234:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:234:in `lazy_load'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:214:in `lookup'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt/mapping.rb:152:in `[]'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt.rb:48:in `[]'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:182:in `block in template_for'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:177:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:177:in `template_for'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:139:in `block (2 levels) in respond_with'
/home/don/.rvm/gems/ruby-2.2.2/gems/tilt-2.0.1/lib/tilt.rb:88:in `fetch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:139:in `block in respond_with'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:115:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:115:in `block in finish'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:114:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:114:in `finish'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-contrib-1.4.6/lib/sinatra/respond_with.rb:159:in `respond_with'
app.rb:18:in `block in <main>'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1610:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1610:in `block in compile!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:974:in `[]'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:974:in `block (3 levels) in route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:993:in `route_eval'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:974:in `block (2 levels) in route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1014:in `block in process_route'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1012:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1012:in `process_route'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:972:in `block in route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:971:in `each'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:971:in `route!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1084:in `block in dispatch!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `block in invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1081:in `dispatch!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:906:in `block in call!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `block in invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `invoke'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:906:in `call!'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:894:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/logger.rb:15:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/commonlogger.rb:33:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:218:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:211:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/show_exceptions.rb:21:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:181:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:2021:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1486:in `block in call'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1795:in `synchronize'
/home/don/.rvm/gems/ruby-2.2.2/gems/sinatra-1.4.6/lib/sinatra/base.rb:1486:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/thin-1.6.3/lib/thin/connection.rb:86:in `block in pre_process'
/home/don/.rvm/gems/ruby-2.2.2/gems/thin-1.6.3/lib/thin/connection.rb:84:in `catch'
/home/don/.rvm/gems/ruby-2.2.2/gems/thin-1.6.3/lib/thin/connection.rb:84:in `pre_process'
/home/don/.rvm/gems/ruby-2.2.2/gems/eventmachine-1.0.8/lib/eventmachine.rb:1062:in `call'
/home/don/.rvm/gems/ruby-2.2.2/gems/eventmachine-1.0.8/lib/eventmachine.rb:1062:in `block in spawn_threadpool'
Tilt 模板引擎注册表似乎默认注册了一大堆引擎。如果我只是显式注册我想要的模板引擎,那么它就会停止尝试加载其他所有内容。
require 'rubygems'
require 'sinatra'
require 'sinatra/respond_with'
require 'tilt/erubis'
set :template_engines, {
:css=>[],
:xml=>[],
:js=>[],
:html=>[:erb],
:all=>[:erb],
:json=>[]
}
get '/' do
@name = 'Bob'
json_data = { :name => @name }
respond_with :'index.html', json_data
end