Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add FAQ

  • Loading branch information...
commit e02e26fe50ac0aad11f5c73ff198b082c666e2e3 1 parent 33c8fb2
Ryan Tomayko rtomayko authored

Showing 2 changed files with 145 additions and 0 deletions. Show diff stats Hide diff stats

  1. +5 0 documentation.markdown
  2. +140 0 faq.markdown
5 documentation.markdown
Source Rendered
@@ -10,6 +10,11 @@ Documentation
10 10
11 11 A whirlwind tour of Sinatra's most interesting features.
12 12
  13 +### [Frequently Asked Questions](faq.html)
  14 +
  15 +Answers to those questions most frequently asked on the mailing list and
  16 +in #sinatra.
  17 +
13 18 ### [Release Notes](changes.html)
14 19
15 20 Detailed change log for each Sinatra release generated from the `CHANGES`
140 faq.markdown
Source Rendered
... ... @@ -0,0 +1,140 @@
  1 +---
  2 +title: "Sinatra: Frequently Asked Questions"
  3 +layout: default
  4 +---
  5 +
  6 +Frequently Asked Questions
  7 +==========================
  8 +
  9 +## <a id='sessions' href='#sessions'>How do I use sessions?</a>
  10 +
  11 +Sessions are disabled by default. You need to enable them and then use the
  12 +`session` hash from routes and views:
  13 +
  14 + enable :sessions
  15 +
  16 + get '/foo' do
  17 + session[:message] = 'Hello World!'
  18 + redirect '/bar'
  19 + end
  20 +
  21 + get '/bar' do
  22 + session[:message] # => 'Hello World!'
  23 + end
  24 +
  25 +## <a id='path_info' href='#path_info'>How do I get the "route" for the current page?</a>
  26 +
  27 +The `request` object probably has what you're looking for:
  28 +
  29 + get '/hello-world' do
  30 + request.path_info # => '/hello-world'
  31 + request.fullpath # => '/hello-world?foo=bar'
  32 + request.url # => 'http://example.com/hello-world?foo=bar'
  33 + end
  34 +
  35 +See [Rack::Request](http://rack.rubyforge.org/doc/classes/Rack/Request.html)
  36 +for a detailed list of methods supported by the `request` object.
  37 +
  38 +## <a id='helpview' href='#helpview'>How do I access helpers from within my views?</a>
  39 +
  40 +Call them! Views automatically have access to all helper methods. In fact,
  41 +Sinatra evaluates routes, views, and helpers within the same exact object
  42 +context so they all have access to the same methods and instance variables.
  43 +
  44 +In `hello.rb`:
  45 +
  46 + helpers do
  47 + def em(text)
  48 + "<em>#{text}</em>"
  49 + end
  50 + end
  51 +
  52 + get '/hello' do
  53 + @subject = 'World'
  54 + haml :hello
  55 + end
  56 +
  57 +In `views/hello.haml`:
  58 +
  59 + %p= "Hello " + em(@subject)
  60 +
  61 +## <a id='partials' href='#partials'>How do I render partials?</a>
  62 +
  63 +Sinatra's template system is simple enough that it can be used for page and
  64 +fragment level rendering tasks. The `erb` and `haml` methods simply return a
  65 +string. However, you need to make sure you disable layout rendering as
  66 +follows:
  67 +
  68 + <%= erb(:mypartial, :layout => false) %>
  69 +
  70 +See [Christopher Schneid](http://www.gittr.com/)'s
  71 +[partials.rb](http://github.com/cschneid/irclogger/blob/master/lib/partials.rb)
  72 +for a more robust partials implementation. It even supports rendering
  73 +collections.
  74 +
  75 +## <a id='multiroute' href='#multiroute'>Can I have multiple URLs trigger the same route/handler?</a>
  76 +
  77 +Sure:
  78 +
  79 + ["/foo", "/bar", "/baz"].each do |path|
  80 + get path do
  81 + "You've reached me at #{request.path_info}"
  82 + end
  83 + end
  84 +
  85 +Seriously.
  86 +
  87 +## <a id='slash' href='#slash'>How do I make the trailing slash optional?</a>
  88 +
  89 +Put a question mark after it:
  90 +
  91 + get '/foo/bar/?' do
  92 + "Hello World"
  93 + end
  94 +
  95 +The route matches `"/foo/bar"` and `"/foo/bar/"`.
  96 +
  97 +## <a id='thindebug' href='#thindebug'>I'm running Thin and an error occurs but there's no output</a>
  98 +
  99 +Try starting Thin with the `--debug` argument:
  100 +
  101 + thin --debug --rackup config.ru start
  102 +
  103 +That should give you an exception and backtrace on `stderr`.
  104 +
  105 +## <a id='email' href='#email'>How do I send Email from Sinatra?</a>
  106 +
  107 +How about a [Pony](http://adam.blog.heroku.com/past/2008/11/2/pony_the_express_way_to_send_email_from_ruby/)
  108 +(`sudo gem install pony`):
  109 +
  110 + require 'pony'
  111 + post '/signup' do
  112 + Pony.mail :to => 'you@example.com',
  113 + :from => 'me@example.com',
  114 + :subject => 'Howdy, Partna!'
  115 + end
  116 +
  117 +You can even use templates to render the body. In `email.erb`:
  118 +
  119 + Good day <%= params[:name] %>,
  120 +
  121 + Thanks for my signing my guestbook. You're a doll.
  122 +
  123 + Frank
  124 +
  125 +And in `mailerapp.rb`:
  126 +
  127 + post '/guestbook/sign' do
  128 + Pony.mail :to => params[:email],
  129 + :from => "me@example.com",
  130 + :subject => "Thanks for signing my guestbook, #{params[:name]}!",
  131 + :body => erb(:email)
  132 + end
  133 +
  134 +<!--
  135 +
  136 +### <a id='queue' href='#queue'>How do I process jobs in the background?</a>
  137 +### <a id='auth' href='#auth'>How do I use HTTP authorization?</a>
  138 +### <a id='auth' href='#auth'>How do I process file uploads?</a>
  139 +
  140 +-->

0 comments on commit e02e26f

Please sign in to comment.
Something went wrong with that request. Please try again.