[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sinatra : examples and FRAME.

B. Randy

4/20/2009 2:43:00 PM

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.
--
Posted via http://www.ruby-....

2 Answers

jonty

4/20/2009 6:29:00 PM

0

B. Randy wrote:
> 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.
>

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
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.287 / Virus Database: 270.12.0/2068 - Release Date: 04/19/09 20:04:00
>
>

B. Randy

4/22/2009 8:12:00 AM

0

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 = "<p> Parameters = #{params} <BR>" +
"start = #{params[:start]} - end = #{params[:end]}"+
" - Type = #{params[:type]} - where = #{params[:where]}</p>" +
"<BR> @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 << "<OPTION>#{opt}</OPTION>"}
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
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=iso-8859-1">
<TITLE>Test of Sinatra.</TITLE>
<h1>Show some data.</h1>
<h2>You must RELOAD the page to see the previous selected values
after the 'SEND' !!! ??? </h2>
</HEAD>
<BODY>
<DIV id='main_form' >
<FORM METHOD="post" action="request_list">
<!-- Call 'select_option()' in the helper to fill the 'SELECT'. -->
<%= select_options() %>
<FIELDSET>
<LEGEND>Make your choice</LEGEND>
<LABEL for="end">Start</LABEL>
<SELECT name="start" >
<OPTION selected value=<%= @start_ini %> > </OPTION>
<%= @start_choice%>
</SELECT>
<LABEL for="end">End</LABEL>
<SELECT name="end">
<OPTION selected value=<%= @end_ini %> > </OPTION>
<%= @start_choice%>
</SELECT>
<LABEL for="where">Where</LABEL>
<SELECT name="where">
<OPTION selected value=<%= @where_ini %> ></OPTION>
<%= @where_choice%>
</SELECT>
<LABEL for="type">Type</LABEL>
<SELECT name="type">
<OPTION selected value=<%= @type_ini %> ></OPTION>
<%= @type_choice%>
</SELECT>
Display all
<INPUT TYPE="checkbox" name="display" value="1" checked></INPUT>
<INPUT type="submit" name="submit" value="Send">
</FIELDSET>
</FORM>
</DIV > <!-- End of the form. ' -->
<DIV>
<h1>Show all data selected.</h1>
<%= @data%>
</DIV>
</BODY>
######################################################################

--
Posted via http://www.ruby-....