Sinatra is ignoring my layout.haml - ruby

Starting a basic Sinatra app. It doesn't seem to be using my layout template. If I put garbage in my layout.haml, I get the Sinatra 500 error page about it not being a properly formed haml file. Running Ruby 1.9.2. on Windows with the gem of Sinatra, Haml, and Rack installed this evening.
App Code:
require 'rubygems'
require 'sinatra'
require 'haml'
set :haml, :format => :html5
get '/' do
"Hello world, it's #{Time.now} at the server!"
end
App's Location / views / layout.haml
%html
%body
= yield
Source of Generated "http://localhost:4567/" Page
Hello world, it's 2011-11-05 02:25:48 -0400 at the server!
^Notice the lack of my layout.

For this purpose you have to say your template engine in action, something like this:
app code:
require 'sinatra'
require 'haml'
get '/' do
haml :hello
end
views/hello.haml:
%p= "Hello world, it's #{Time.now} at the server!"
views/layout.haml:
%html
%body
= yield

Related

Sinatra I18n Fallbacks using Rack::Locale

I'm trying to set up a simple Sinatra app with I18n, following the recommended Sinatra recipe, and using Rack:Locale to determine the language.
My app.rb:
require 'rubygems'
require 'sinatra'
require 'rack/contrib'
require 'i18n'
require 'i18n/backend/fallbacks'
require 'tilt/haml'
use Rack::Locale
configure do
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.load_path = Dir[File.join(settings.root, 'locales', '*.yml')]
I18n.backend.load_translations
end
helpers do
def t(*args)
I18n.t(*args)
end
end
get '/' do
haml :index
end
My locales/en.yml:
en:
welcome: "Welcome!"
When I run rackup and visit the root path of my Sinatra app, I get the following:
I18n::InvalidLocale at /
"en-US" is not a valid locale
file: i18n.rb location: enforce_available_locales! line: 284
I thought that the I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) would handle this, by not finding en-US and falling back to en (which I have), but apparently not. What am I missing?
Add:
I18n.enforce_available_locales = false

No perfomance gains from using em-http-request

I'm trying to understand how to use various non-blocking IO libraries in Ruby and made a simple app for testing using Sinatra,
# proxy.rb
require 'bundler/setup'
require 'sinatra/base'
require 'sinatra/synchrony'
require 'faraday'
class ProxyApp < Sinatra::Base
register Sinatra::Synchrony
get "/proxy" do
conn = Faraday.new("http://mirror.yandex.ru") do |faraday|
faraday.use Faraday::Adapter::EMSynchrony
end
conn.get "/ubuntu-releases/precise/ubuntu-12.04.1-alternate-i386.iso"
"Hello, world"
end
get "/" do
"Hello, world"
end
end
As far as I understand, downloading a file using non-blocking IO should allow other requests to execute, but it doesn't - if I'm using ab to open /proxy path (I'm using Thin as an app server), request to / takes a very long time. Am I doing something wrong?
Sinatra::Synchrony? Why?
config.ru:
require File.join Dir.pwd, 'proxy.rb'
run Proxy
Gemfile:
source 'https://rubygems.org'
gem 'sinatra'
gem 'thin'
gem 'faraday'
gem 'em-synchrony'
gem 'em-http-request'
gem 'rack-fiber_pool'
proxy.rb:
require 'bundler'
Bundler.require
class Proxy < Sinatra::Base
use Rack::FiberPool
get "/proxy" do
conn = Faraday.new("http://mirror.yandex.ru") do |faraday|
faraday.use Faraday::Adapter::EMSynchrony
end
conn.get "/ubuntu-releases/precise/ubuntu-12.04.1-alternate-i386.iso"
"Hello, world"
end
get "/" do
"Hello, world"
end
end
Start:
thin start -d
wget localhost:3000/proxy
In another terminal:
wget localhost:3000/
The reply is immediate for /, no matter how many requests to /proxy you do in parrallel.

use haml template in Sinatra framework

How to create a page using Sinatra which will use my prepared index.html.haml template.
Let's say i have following code:
require 'sinatra'
require 'sass'
require 'haml'
get '/' do
haml :index ???
end
my index.html.haml file is located in /sinatraapp/haml/index.html.haml
You need to tell sinatra where your views are located - see this
require 'sinatra'
require 'sass'
require 'haml'
set :views, "path/to/your/haml/dir"
get '/' do
haml :index
end

Sinatra HAML Heroku in-file templates

I'm following a tutorial from http://ruby.about.com/od/sinatra/a/sinatra7_2.htm however I'm having a few problems running the app within my own environment.
The problem is that the following line:
haml :list, :locals => { :cs => Contact.all }
results in a "No such file or directory - [...]/views/list.haml"
The HAML template is within the file, and terminated by:
__END__
## layout
however ruby seems to be looking in the views/ directory for the Haml files.
Is this tutorial missing a call to force ruby to look inside the file, or this resource suggests that in-file templates are broken for version 1.9.2.
I'm using sinatra version 1.1.2 and ruby 1.8.7.
I can't reproduce with Sinatra 1.1.2 and Ruby 1.9.2.
So something like this (sinatrarb.com) example generates the error?
require 'sinatra'
get '/' do
haml :index
end
__END__
## layout
%html
= yield
## index
%div.title Hello world!!!!!
calling a list template you will also need add it to the end of your file:
require 'sinatra'
get '/' do
haml :list
end
__END__
## layout
%html
= yield
## list
%div.title the LIST

deploy a sinatra app with passenger gives only 404, page not founds. Yet a simple rack app works

I have correctly (or prbably not) installed passenger on apache 2. Rack works, but sinatra keeps giving 404's.
Here is what works:
config.ru:
#app = proc do |env|
return [200, { "Content-Type" => "text/html" }, "hello <b>world</b>"]
end
run app
Here is what works too:
Running the app.rb (see below) with ruby app.rb and then looking at localhost:4567/about and /
restarting the app, gives me a correct hello world. w00t.
But then there is the sinatra entering the building:
config.ru
require 'rubygems'
require 'sinatra'
root_dir = File.dirname(__FILE__)
set :environment, ENV['RACK_ENV'].to_sym
set :root, root_dir
set :app_file, File.join(root_dir, 'app.rb')
disable :run
run Sinatra::Application
and an app.rb
require 'rubygems'
require 'sinatra'
get '/' do
"Hallo wereld!"
end
get '/about' do
"Hello world, it's #{Time.now} at the server!"
end
This keeps giving 404s.
/var/logs/apache2/error.log lists these correctly as "404" with something that worries me:
83.XXXXXXXXX - - [30/May/2010 16:06:52] "GET /about " 404 18 0.0007
83.XXXXXXXXX - - [30/May/2010 16:06:56] "GET / " 404 18 0.0007
The thing that worried me, is the space after the / and the /about. Would apache or sinatra go looking for /[space], like /%20?
If anyone knows what this problem relates to, maybe a known bug (that I could not find) or a known gotcha?
Maybe I am just being stupid and getting "it all wrong?"
Otherwise any hints on where to get, read or log more developers data on a running rack, sinatra or passenger app would be helpfull too: to see what sinatra is looking for, for example.
Some other information:
Running ubuntu 9.04, apache2-mm-prefork (deb), mod_php5, ruby 1.8.7, passenger 2.2.11, sinatra 1.0
You are not loading the routes in app.rb. To do this, replace require 'sinatra' with require File.join(File.dirname(__FILE__), 'app.rb') in config.ru.
root_dir = File.dirname(__FILE__)
app_file = File.join(root_dir, 'app.rb')
require app_file
set :environment, ENV['RACK_ENV'].to_sym
set :root, root_dir
set :app_file, app_file
disable :run
run Sinatra::Application
set :app_file won't load them for you.
Just substitute the require sinatra with a require 'app' and you're set to go.

Resources