* Sinatra is simply a Ruby Gem that is build specifically for easy web development. Its syntax is generally build around blocks like one below.
get '/' do
@value = 'abc'
erb :layout
end
-sinatra route derived its name from HTTP method like GET, POST, or PUT.
- here, '/' means a route for the web application like www.yourapp.com/ , it just specify the last character '/' for brevity.
- any instance variable like @value will be accessible inside the templating language , in this case, erb file.
- The last line 'erb :layout' will ask Sinatra to execute the template :layout
* There are two important variable in Sinatra, namely params and session
- session is instructing Sinatra to build a cookie that holds important information/build states that will last between multiple HTTP request, since HTTP is a stateless communication (ie. it forgets after each HTTP request).
- You can push any information any information into session because it is just a hash. eg:
session[:success] = "Successful ! "
- You have to enable session before using it like this:
configure do
enable :sessions
set :session_secret, 'secret'
end
- it appears to me that session is unique between route, but i could be wrong.
* params
- params is a universal variable that is unique inside each sinatra route block
get "/link/:id/:num" do
@value = params[:num]
erb :layout
end
get '/' do
@value = 'abc'
erb :layout
end
-sinatra route derived its name from HTTP method like GET, POST, or PUT.
- here, '/' means a route for the web application like www.yourapp.com/ , it just specify the last character '/' for brevity.
- any instance variable like @value will be accessible inside the templating language , in this case, erb file.
- The last line 'erb :layout' will ask Sinatra to execute the template :layout
* There are two important variable in Sinatra, namely params and session
- session is instructing Sinatra to build a cookie that holds important information/build states that will last between multiple HTTP request, since HTTP is a stateless communication (ie. it forgets after each HTTP request).
- You can push any information any information into session because it is just a hash. eg:
session[:success] = "Successful ! "
- You have to enable session before using it like this:
configure do
enable :sessions
set :session_secret, 'secret'
end
- it appears to me that session is unique between route, but i could be wrong.
* params
- params is a universal variable that is unique inside each sinatra route block
get "/link/:id/:num" do
@value = params[:num]
erb :layout
end
- This will create a params hash of { :id = "value1", :num = "value2" }
- params is unique for each route block.
- if you have a form, the form input will have a data inside the params hash.
www.yourapp.com/link?name=john
The above URL contains a query and will create a params hash with a key of 'name' and value of 'john'.
Comments
Post a Comment