Discussion:
simple example to get and send a value
'Stéphane Laurent' via sinatrarb
2015-06-21 15:12:48 UTC
Permalink
Hello,

I'd like to have a simple app example that gets a value from the client and
send a value to the client.
For example, an app taking a value and sending the double :

require 'sinatra'

get '/double' do
erb :html
end

??????
x = params[:x]
result = 2*x
send result to "#result"
??????

__END__

@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"></div>
</form>
</body>
</html>
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
BeeRich33
2015-06-22 04:38:45 UTC
Permalink
Your form doesn't start anywhere.
Post by 'Stéphane Laurent' via sinatrarb
Hello,
I'd like to have a simple app example that gets a value from the client
and send a value to the client.
require 'sinatra'
get '/double' do
erb :html
end
??????
x = params[:x]
result = 2*x
send result to "#result"
??????
__END__
@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"></div>
</form>
</body>
</html>
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Stéphane Laurent' via sinatrarb
2015-06-23 06:08:09 UTC
Permalink
That does not help me. I have absolutely no idea about how to do. I don't
find an easy example on the web, only complicated ones.
Post by BeeRich33
Your form doesn't start anywhere.
Post by 'Stéphane Laurent' via sinatrarb
Hello,
I'd like to have a simple app example that gets a value from the client
and send a value to the client.
require 'sinatra'
get '/double' do
erb :html
end
??????
x = params[:x]
result = 2*x
send result to "#result"
??????
__END__
@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"></div>
</form>
</body>
</html>
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Stéphane Laurent' via sinatrarb
2015-06-23 07:29:31 UTC
Permalink
And the code I posted was not intended to work. This is just a "draft" to
show what I want.
Post by BeeRich33
Your form doesn't start anywhere.
Post by 'Stéphane Laurent' via sinatrarb
Hello,
I'd like to have a simple app example that gets a value from the client
and send a value to the client.
require 'sinatra'
get '/double' do
erb :html
end
??????
x = params[:x]
result = 2*x
send result to "#result"
??????
__END__
@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"></div>
</form>
</body>
</html>
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
DAZ
2015-07-02 06:28:05 UTC
Permalink
Your code is very close to working - you just need an action attribute in
the form element to say where to post the form to and then a 'post' route
handler to deal with it. Then you need to store the result in an instance
variable called @result. I've made the changed below that should work.

This post might help
you http://ididitmyway.herokuapp.com/past/2010/1/10/project_1_reverse/

Hope this helps,

DAZ

require 'sinatra'

get '/double' do
erb :html
end

post '/double'
x = params[:x]
@result = 2*x
erb :html
end


__END__

@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<form action="/double">
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"><%= result %></div>
</form>
</body>
</html>
Post by 'Stéphane Laurent' via sinatrarb
Hello,
I'd like to have a simple app example that gets a value from the client
and send a value to the client.
require 'sinatra'
get '/double' do
erb :html
end
??????
x = params[:x]
result = 2*x
send result to "#result"
??????
__END__
@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"></div>
</form>
</body>
</html>
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'laurent stephane' via sinatrarb
2015-07-02 19:16:40 UTC
Permalink
Thank you ! "@result" is what I was looking for. And some examples such as your link, perfect ! 


Le Jeudi 2 juillet 2015 8h28, DAZ <***@gmail.com> a écrit :


Your code is very close to working - you just need an action attribute in the form element to say where to post the form to and then a 'post' route handler to deal with it. Then you need to store the result in an instance variable called @result. I've made the changed below that should work.
This post might help you http://ididitmyway.herokuapp.com/past/2010/1/10/project_1_reverse/
Hope this helps,
DAZ
require 'sinatra'
 
get '/double' do
  erb :html
end
 
post '/double'
 x = params[:x]
 @result = 2*x
 erb :htmlend

 
__END__
 
@@html
<!doctype html>
<html>
  <header>
    <title>Double</title>
  </header>
  <body>     <form action="/double">
      <p>Enter x:</p>
      <input type="number" name="x">
      <input type="submit" value="Calculate 2*x">
      <p>Result:</p>
      <div id="result"><%= result %></div>
    </form>
  </body>
</html>

On Sunday, June 21, 2015 at 4:36:30 PM UTC+1, Stéphane Laurent wrote:
Hello, 
I'd like to have a simple app example that gets a value from the client and send a value to the client. For example, an app taking a value and sending the double : 
require 'sinatra'
 
get '/double' do
  erb :html
end
 
??????
 x = params[:x]
 result = 2*x
 send result to "#result"
??????
 
__END__
 
@@html
<!doctype html>
<html>
  <header>
    <title>Double</title>
  </header>
  <body>
      <p>Enter x:</p>
      <input type="number" name="x">
      <input type="submit" value="Calculate 2*x">
      <p>Result:</p>
      <div id="result"></div>
    </form>
  </body>
</html>
--
You received this message because you are subscribed to a topic in the Google Groups "sinatrarb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sinatrarb/DaqBLTV352k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
DAZ
2015-07-04 21:37:55 UTC
Permalink
Glad you found it useful!

Feel free to ask about anything else if you need it.

Cheers,

DAZ
Post by 'laurent stephane' via sinatrarb
Thank you !
perfect !
Your code is very close to working - you just need an action attribute in
the form element to say where to post the form to and then a 'post' route
handler to deal with it. Then you need to store the result in an instance
This post might help you
http://ididitmyway.herokuapp.com/past/2010/1/10/project_1_reverse/
Hope this helps,
DAZ
require 'sinatra'
get '/double' do
erb :html
end
post '/double'
x = params[:x]
@result = 2*x
erb :html
end
__END__
@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<form action="/double">
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"><%= result %></div>
</form>
</body>
</html>
Hello,
I'd like to have a simple app example that gets a value from the client
and send a value to the client.
require 'sinatra'
get '/double' do
erb :html
end
??????
x = params[:x]
result = 2*x
send result to "#result"
??????
__END__
@@html
<!doctype html>
<html>
<header>
<title>Double</title>
</header>
<body>
<p>Enter x:</p>
<input type="number" name="x">
<input type="submit" value="Calculate 2*x">
<p>Result:</p>
<div id="result"></div>
</form>
</body>
</html>
--
You received this message because you are subscribed to a topic in the
Google Groups "sinatrarb" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sinatrarb/DaqBLTV352k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...