Sinatra HAML Heroku in-file templates - heroku

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

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

Sinatra exits without error

I'm very new to Sinatra, and I'm trying to get asset management & compiling working according to this article. Here is my main file so far:
require 'sinatra/base'
require 'sinatra/assetpack'
require 'sass'
class App < Sinatra::Base
register Sinatra::AssetPack
assets do
css :application, [
'/css/main.scss'
]
css_compression :sass
end
get '/hi' do
erb "Hello World!"
end
end
but, for some reason, when I run ruby main.rb, it just exits without failure or anything. Is there a special keyword to get the application to serve files?
Using the modular style of Sinatra application, as you are doing, running ruby main.rb is going to exit without error because it is being treated as a standard ruby application and no webserver is ever created.
You have two options.
1 Add run! if app_file == $0 just before the final end statement in your example.
This will allow you to run the app with ruby main.rb
2 (This is the preferred method) Create a rackup file config.ru with the following contents.
require './main.rb'
run App
Now you can serve the application with the command rackup -p 4567 where 4567 is whatever port number you want to use.
You need to start the application
require 'sinatra/base'
require 'sinatra/assetpack'
require 'sass'
class App < Sinatra::Base
register Sinatra::AssetPack
assets do
css :application, [
'/css/main.scss'
]
css_compression :sass
end
get '/hi' do
erb "Hello World!"
end
run! if app_file == $0
end
one observation, erb should point to a template, example:
get '/hi' do
erb :home
end
should look for a file in ../views/home.erb
Also Assuming you already did gem install sinatra. I would also use the rerun gem while developing in sinatra, gem install rerun then rerun ruby app.rb. Rerun will reload your project when you make changes to your code so you won't have to restart the app when ever you make a change.

Ruby URI module oddness in modular Sinatra app

I'm having trouble using the Ruby URI module's encode_www_form method in a modular Sinatra app. For some reason, URI is interpreted as being the URI::Parser subclass, and so the method call understandably fails.
I've reduced this to a minimal test case. The Gemfile:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'sinatra'
And app.rb:
require 'sinatra/base'
class Frontend < Sinatra::Base
get '/test/' do
URI.encode_www_form(:a => 1, :b => 2)
end
run! if app_file == $0
end
If I then run ruby app.rb and access /test/ I get:
NoMethodError - undefined method `encode_www_form' for #<URI::Parser:0x007fa9221ca868>:
app.rb:6:in `block in <class:Frontend>'
If I convert it to a classic-style Sinatra app, so that app.rb is like this:
require 'sinatra'
get '/test/' do
URI.encode_www_form(:a => 1, :b => 2)
end
Then call ruby app.rb and access /test/, the page shows "a=1&b=2" as desired.
So what's going wrong in the modular format that means something's up with URI?
The class Sinatra::Base redefines URI on line 856 of https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb, which is why your URI reference gets evaluated as that value.
If you want to avoid this problem, you can change your reference to ::URI.
As of Sinatra 1.4.4, the URI module is no longer overwritten.
I tried to reproduce this in irb. This may sound stupid, but require 'uri' did the trick there.

Setting Sinatra template options works locally, broken on Heroku

My Sinatra web site uses an admittedly ghetto way of switching between two languages, which works perfectly on my local machine, but not on Heroku.
My app.rb is as follows:
require 'sinatra'
get '/' do
set :erb, :views => settings.views + "/en"
erb :index, :layout => false, :views => settings.views
end
get '/tr' do
set :erb, :views => settings.views + "/tr"
redirect to('/home')
end
get '/en' do
set :erb, :views => settings.views + "/en"
redirect to('/home')
end
get '/home' do
erb :home
end
get '/products' do
erb :products
end
...
When I click on the links that go to the /en and /tr urls, I am correctly redirected to the /home link, but the views folder does not change.
My gems are the same versions in both environments:
rack (1.4.1)
rack-protection (1.2.0)
tilt (1.3.3)
sinatra (1.3.3)
bundler (1.3.2)
Link to the site
Help, please.
I suggest replacing your admittedly ghetto way with something more robust like the i18n gem
here is a tutorial
And instead of using the browser's language (in example) you could do something like this in a before filter
before do
case request.path_info
when /^\/tr/ then set :locale, "tr"
else set :locale, "en"
end
end
and in helpers
helpers do
def get_locale
settings.locale
end
# other helpers from example
end
That way you only need one copy of your view templates, reducing the complication of changing all views when you only need to change one element (for example)
If you want to test your sinatra app like it will act on heroku you can throw on a RACK_ENV=production before starting your server. Ex. RACK_ENV=production ruby my_app.rb

Sinatra is ignoring my layout.haml

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

Resources