/ / Sinatra tenta carregar o mecanismo haml quando eu uso o RespondWith - ruby, sinatra

Sinatra tenta carregar o mecanismo haml quando eu uso o RespondWith - ruby, sinatra

Eu quero que o mesmo URL responda com HTML ou JSON, dependendo da solicitação "s Accept header, então estou tentando usar o RespondWith extension de Sinatra :: Contrib.

Sempre que eu forneço um nome de modelo para respond_with(), Eu recebo um erro. O erro parece ocorrer quando ele tenta carregar o mecanismo de modelo Haml.

Eu só quero usar o mecanismo de modelo Erb, então como desabilitar o mecanismo de modelo Haml?

O erro que vejo é, "LoadError - não é possível carregar tal arquivo - haml".

Aqui está o app.rb arquivo que executa o servidor da 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

Aqui está o arquivo de modelo views/index.html.erb:

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

Aqui está o rastreamento completo da pilha do erro que recebo quando solicito HTML ou 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"

Respostas:

1 para resposta № 1

Parece que o registro do mecanismo de modelo Tilttem um monte de motores registrados por padrão. Se eu apenas registrar explicitamente os mecanismos de modelo que quero, ele pára de tentar carregar todo o resto.

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