title | layout |
---|---|
Sinatra: Frequently Asked Questions |
default |
Sessions are disabled by default. You need to enable them and then use the
session
hash from routes and views:
enable :sessions
get '/foo' do
session[:message] = 'Hello World!'
redirect '/bar'
end
get '/bar' do
session[:message] # => 'Hello World!'
end
Not yet. Rack is not yet Ruby 1.9 compatible and Sinatra is very much dependent on Rack. We are in the early stages of testing under 1.9 and we're reviewing all new code and idioms for potential 1.9 compatibility issues. The 1.0 release (currently scheduled for mid 2009) will run on 1.9 and chances are good that we'll support 1.9 before then.
The request
object probably has what you're looking for:
get '/hello-world' do
request.path_info # => '/hello-world'
request.fullpath # => '/hello-world?foo=bar'
request.url # => 'http://example.com/hello-world?foo=bar'
end
See Rack::Request
for a detailed list of methods supported by the request
object.
Call them! Views automatically have access to all helper methods. In fact, Sinatra evaluates routes, views, and helpers within the same exact object context so they all have access to the same methods and instance variables.
In hello.rb
:
helpers do
def em(text)
"<em>#{text}</em>"
end
end
get '/hello' do
@subject = 'World'
haml :hello
end
In views/hello.haml
:
%p= "Hello " + em(@subject)
Sinatra's template system is simple enough that it can be used for page and
fragment level rendering tasks. The erb
and haml
methods simply return a
string. However, you need to make sure you disable layout rendering as
follows:
<%= erb(:mypartial, :layout => false) %>
See Chris Schneider's partials.rb for a more robust partials implementation. It even supports rendering collections.
Sure:
["/foo", "/bar", "/baz"].each do |path|
get path do
"You've reached me at #{request.path_info}"
end
end
Seriously.
Put a question mark after it:
get '/foo/bar/?' do
"Hello World"
end
The route matches "/foo/bar"
and "/foo/bar/"
.
Sinatra apps do not typically have a very complex file heirarchy under
views
. First, consider whether you really need subdirectories at all.
If so, you can use the views/foo/bar.haml
file as a template with:
get '/' do
haml :'foo/bar'
end
This is basically the same as sending #to_sym
to the filename and can also
be written as:
get '/' do
haml 'foo/bar'.to_sym
end
Try starting Thin with the --debug
argument:
thin --debug --rackup config.ru start
That should give you an exception and backtrace on stderr
.
How about a Pony
(sudo gem install pony
):
require 'pony'
post '/signup' do
Pony.mail :to => '[email protected]',
:from => '[email protected]',
:subject => 'Howdy, Partna!'
end
You can even use templates to render the body. In email.erb
:
Good day <%= params[:name] %>,
Thanks for my signing my guestbook. You're a doll.
Frank
And in mailerapp.rb
:
post '/guestbook/sign' do
Pony.mail :to => params[:email],
:from => "[email protected]",
:subject => "Thanks for signing my guestbook, #{params[:name]}!",
:body => erb(:email)
end
See the book.
Include Rack::Utils
in your helpers and create an h
alias as follows:
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
Now you can escape html in your templates like this:
<%= h scary_output %>
Thanks to Chris Schneider for the tip!