Sinatra : examples and FRAME

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 :slight_smile:

Thanks.

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 :slight_smile:

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

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 :frowning:

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%>
######################################################################