What's Sinatra's equivalent of Rails' redirect_to
method? I need to follow a Post/Redirect/Get flow for a form submission whilst preserving the instance variables that are passed to my view. The instance variables are lost when using the redirect
method.
views:
416answers:
2The Sinatra Book should clear your question. Especially the "Redirect" part.
Quoted from the book:
The redirect actually sends back a Location header to the browser, and the browser makes a followup request to the location indicated. Since the browser makes that followup request, you can redirect to any page, in your application, or another site entirely.
The flow of requests during a redirect is: Browser –> Server (redirect to ’/’) –> Browser (request ’/’) –> Server (result for ’/’)
Hi John,
Redirect in Sinatra is the most simple to use.
So the above code can explain
require 'rubygems'
require 'sinatra'
get '/' do
redirect "http://yourawesomeurlhere.com
end
you can also redirect to another path in your current application like
(this sample with a delete method)
delete /delete_post' do
redirect '/list_posts'
end
A very common place where this redirect instruction is used is under Authentication
def authorize!
redirect '/login' unless authorized?
end
you can check more samples under:
Sinatra Manual: http://sinatra-book.gittr.com/
Faq: http://www.sinatrarb.com/faq.html
Extensions: http://www.sinatrarb.com/extensions.html
About your second question, passing variables into views, yep it's possible
so,
get '/pizza/:id' do
# makeing lots of pizza
@foo = Foo.find(params[:id])
erb '%h1= @foo.name'
end
have fun