/ / Sinatra intenta cargar el motor Haml cuando uso RespondWith - ruby, sinatra

Sinatra intenta cargar el motor Haml cuando uso RespondWith - ruby, sinatra

Quiero que la misma URL responda con HTML o JSON, dependiendo del encabezado Aceptar de la solicitud, por lo que estoy tratando de usar el RespondWith extensión de Sinatra :: Contrib.

Cada vez que proporciono un nombre de plantilla para respond_with(), Me sale un error. El error parece ocurrir cuando intenta cargar el motor de plantillas Haml.

Solo quiero usar el motor de plantillas Erb, ¿cómo deshabilito el motor de plantillas Haml?

El error que veo es "LoadError - no se puede cargar dicho archivo - haml".

Aquí esta la app.rb Archivo que ejecuta el servidor web:

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

Aquí está el archivo de plantilla views/index.html.erb:

<html>
<body>
<h1>Index Page</h1>
<h2><%= @name %></h2>
</body>
</html>

Aquí está el seguimiento completo de la pila del error que recibo cuando solicito HTML o 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"

Respuestas

1 para la respuesta № 1

Parece que el registro del motor de plantillas Tilttiene un montón de motores registrados por defecto. Si solo registro explícitamente los motores de plantilla que quiero, entonces deja de intentar cargar todo lo demás.

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