# Sinatra : examples and FRAME

**URL:** <https://www.ruby-forum.com/t/sinatra-examples-and-frame/165983>\
**Category:** Ruby\
**Created:** [April 20, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/sinatra-examples-and-frame/165983 "2009-04-20T16:42:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![randy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@randy](https://www.ruby-forum.com/u/randy)\
**Post date:** [April 20, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/sinatra-examples-and-frame/165983/1 "2009-04-20T16:42:34Z")

</div>

Hi,  
I’m working on Sinatra for days. The ‘book’ and the ‘FAQ’ are clear but  
haven’t real examples.

I use only “dbi” for database, I’m using Oracle with real tables.

On a page, I have a FORM and below I would see the result of the  
request.

I have tried something like :

- I use a session;
- put the form in the main page ‘/’, the form make a “post ‘action’”;
- make a " get ‘/’";
- make a “post ‘action’”, work on data, " redirect ‘/’". The selected  
value in the form are lost. I must make a reload of the page to see the  
selected values of the form.

This is my problems :

- How to have a form AND the result of this form in the same page ?
- How to recall the values taken from the form in the form ?
- May be using some frames is a good idea but I don’t see how to define  
the “src” attribute ?

Well, some have a real and SIMPLE example of a web application for  
Sinatra ? or could help me to find a solution ?

I’ll take the ideas 🙂

Thanks.

---

<div class="post-metadata">

**Author:** ![randy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@randy](https://www.ruby-forum.com/u/randy)\
**Post date:** [April 20, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/sinatra-examples-and-frame/165983/2 "2009-04-20T20:29:18Z")

</div>

B. Randy wrote:

> - I use a session;
> - May be using some frames is a good idea but I don’t see how to define  
> the “src” attribute ?
> 
> Well, some have a real and SIMPLE example of a web application for  
> Sinatra ? or could help me to find a solution ?
> 
> I’ll take the ideas 🙂
> 
> Thanks.

I don’t see the need to use frames, it is just a question of saving your  
data in the post method and reading your data in the get

The get will fetch the current data save it in an instance variable  
(@variable) and refer to a template that includes your form which has  
an href of ‘/’ and method post  
but can also display the current data from the @variable.

Hope this is of help

---

<div class="post-metadata">

**Author:** ![randy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@randy](https://www.ruby-forum.com/u/randy)\
**Post date:** [April 22, 2009, 10:11am UTC](https://www.ruby-forum.com/t/sinatra-examples-and-frame/165983/3 "2009-04-22T10:11:47Z")

</div>

jonty wrote:

> I don’t see the need to use frames, it is just a question of saving your  
> data in the post method and reading your data in the get
> 
> The get will fetch the current data save it in an instance variable  
> (@variable) and refer to a template that includes your form which has  
> an href of ‘/’ and method post  
> but can also display the current data from the @variable.

Hello,

‘@variable’ isn’t useful because they aren’t global. The global variable  
like ‘@@variable\_global’ could be used in a Web context ? What’s happen  
when many user play with the application ?

I’ve tried to follow your advice in the following code. Yes, I know that  
I 'm not good with HTML ☹

I’ve put comments in the code.

I can’t show the selected value in the form without reload the page !?

Suggestions ?

Tkanks.

Randy11.

######################################################################

# demo.rb

require ‘sinatra’

# What is it done ?

# 1 - The “get ‘/’” show the form. In the form the method ‘POST’ is used

# and call ‘request\_list’.

# 2 - The "post ‘/request\_list’ gets the parameters, save it in

session’s

# variables.

# 3 - The “post” redirect to get “’/:start,:end,:type,:where’” which

# build the data : “@data”, and display again the form.

# =\> PROBLEM : I lost the selected value in the form. I can’t set the

# default value like : ‘\<OPTION selected value = \<% @start\_ini %\> \>’.

enable :sessions

# configure directives can be used to set constants

# that are available in each of your views

# DON’T FORGET TO STOP AND START THE APPLICATION IF THIS AREA IS

MODIFIED.  
configure do  
Version = Sinatra::VERSION  
@data = “”  
end

get ‘/’ do  
erb :main\_html  
end

get ‘/:start,:end,:type,:where’ do  
@start\_ini = “’#{params[:start]}’”  
@end\_ini = “’#{params[:end]}’”  
@type\_ini = “#{params[:type]}”  
@where\_ini = “#{params[:where]}”

# The ‘data’ are very big, so ‘cookie’ or ‘session’ can’t be used

# for share ‘data’ between methods ‘get’ or ‘post’ …

# May be I must call an external method from a class.

# Any other idea ?

@data = "

Parameters = #{params}   
" +  
“start = #{params[:start]} - end = #{params[:end]}”+  
" - Type = #{params[:type]} - where = #{params[:where]}

" +  
"  
 @start\_ini = #{@start\_ini}, @end\_ini = #{@end\_ini}, " +  
“@type\_ini = #{@type\_ini}, @where\_ini = #{@where\_ini}”
# I must reload de page to see de default values ‘…\_ini’, then they

are

# shown by ‘@data’. Why ?

erb :main\_html  
end

post ‘/request\_list’ do  
session[:start] = params.fetch(“start”).to\_i  
session[:end] = params.fetch(“end”).to\_i  
session[:where] = params.fetch(“where”)  
session[:type] = params.fetch(“type”)  
session[:display] = params.fetch(“display”).to\_i

# To avoid miss understanding with my code.

session[:where] = nil if (session[:where] == “nil”)  
session[:type] = nil if (session[:type] == “nil”)

# Ensure ‘start’ \< ‘end’.

if (session[:start] \> session[:end])  
session[:start], session[:end] = session[:end], session[:start]  
end

redirect “/#{session[:start]},#{session[:end]},”  
“’#{session[:type]}’,’#{session[:where]}’”  
end

# My ‘helpers’

helpers do

# Build the HTML code from an array of option for ‘SELECT’.

def html\_select(array)  
option=Array.new()  
array.each {|opt| option \<\< “#{opt}”}  
option  
end

# The list of choices.

def select\_options  
@start\_choice = html\_select([1, 2, 3, 4])  
@where\_choice = html\_select(%w[Here There Nowhere Somewhere])  
@type\_choice = html\_select(%w[A B C D E F G])  
# If nothing is displayed like : “”, the last thing, here :  
# ‘@type\_choice’, is displayed.  
“”  
end  
end

# All HTML pages here.

**END**

@@ main\_html

 Test of Sinatra.
# Show some data.

## You must RELOAD the page to see the previous selected values after the 'SEND' !!! ???

 \<%= select\_options() %\> Make your choice Start \> \<%= @start\_choice%\> End \> \<%= @start\_choice%\> Where \> \<%= @where\_choice%\> Type \> \<%= @type\_choice%\> Display all 

# Show all data selected.
 \<%= @data%\> 
######################################################################
