How to do a Post/Redirect/Get using Sinatra?

Redirect in Sinatra is the most simple to use.

So the code below can explain:

require 'rubygems'
require 'sinatra'

get "https://stackoverflow.com/" do
  redirect "http://example.com"
end

You can also redirect to another path in your current application like this, though this sample will delete a 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 see more samples under:

Sinatra Manual

FAQ

Extensions

As for your second question, passing variables into views, it’s possible like this:

get '/pizza/:id' do
  # makeing lots of pizza
  @foo = Foo.find(params[:id])
  erb '%h1= @foo.name'
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)