Ruby+Tk+HTML Example

T

takaoueda

The following is a simple example of how Erb can be used
to integrate HTML, Tk, and Ruby.

I put this program in the \apache\cgi-bin\ directory of Apache
1.3, and tested by directly calling http://localhost/cgi-bin
/erbtest.rb with etscape 4.75 and IE 6.0 Win98SE with the
newest Ruby 1.8.2-14RC9 executable for Windows. I experienced:

..Apache 2.0 does not work with Win98SE.
..Both IE and Netscape do not display files of the mime type
x-httpd-eruby. Therefore, I had to give up the use of
so-called embedded Erb with the file extension .rhtml and
the addition of AddType application and Action application
to the Apache conf file.
..Tcl/Tk with Erb failed with Apache with a message of unfound
"tcltklib" with the older Ruby 1.8.1 for Windows.
..The "malformed header" error occurs if the second line is
missing.
..The comment on the first line is displayed on the Web page.
A solution to this problem was given by Carlos in another
message.
..If the Tk.mainloop is exited without printing anything,
"The document has no data" error message is given by Netscape.
..In order to avoid the Dos windows being stuck or instantly
closed during debugging, I opened Tool of Windows Explorer and
edited File Types so that, in my case, files of the file types
..rb and .erb should be respectively opened with command.com /k
"E:\ruby\bin\ruby.exe" %1 and command.com /k E:\ruby\bin\erb.bat"
%1 and used Windows Explorers or editors to run the programs.

Test program (erbtest.erb)
#! E:/ruby182/ruby/bin/erb.bat
<%print "Content-type: text/html\r\n\r\n"%>

<html><body>
<form method=post enctype=application/x-www-form-urlencoded>
<hr><h1>A Form: </h1>
<textarea cols =50 name=get_text rows=5></textarea>
<br><input type=submit></form>
<%con_length=ENV['CONTENT_LENGTH'].to_i%>
<%=content=STDIN.read(con_length)%>

<%require 'tk'%>
<%if content =~ /tk\+test/%>
<%TkButton.new {%>
<% text "Ok"%>
<% command proc { p "Thank you"; exit }%>
<%pack { side 'left'; padx 20; pady; 20 }%>
<%}%>
<%Tk.mainloop%>
<%end%>
</body></html>
 
T

takaoueda

First, a correction:
Change "Both of ... do not display ..." into "None of ... displays ..."

Second, the Tk button is displayed on the server's screen. Therefore,
an Erb program of this kind can be used for a real-time communication.
 
T

takaoueda

First, a correction:
Change "Both of ... do not display ..." into "None of ... displays ..."

Second, the Tk button is displayed on the server's screen. Therefore,
an Erb program of this kind can be used for a real-time communication.
 
T

Takao Ueda

It seems the syntax of Ruby+Tk in Erb is more rigorous
than in Ruby. Here is another snippet that works.
This is a substitute for the inside of if-end statement
in the previous example.

<%message = TkVariable.new%>
<%"padx 20; pady 20"%>
<%TkLabel.new {%>
<%text 'Enter a message:'%>
<%pack {ph}%>
<%}%>
<%entry = TkEntry.new{%>
<%textvariable message%>
<%}%>
<%entry.pack{ ph }%>
<%TkButton.new {%>
<%text content%>
<% command proc {p "Thank you"; exit}%>
<%pack { ph }%>
<%}%>
<%TkButton.new {%>
<%text 'Exit'%>
<% command proc {p message.value; exit}%>
<% pack { ph }%>
<%}%>

<%Tk.mainloop%>

Takao
 
T

takaoueda

We can also use TkText to display the client's message
on the server's screen as
<%txt=TkText.new.pack%>
<%txt.insert('end', content)%>

Takao
 
T

takaoueda

We can also use class CGI within
an Erb program.

#! E:/ruby182/ruby/bin/erb.bat
<%print "Content-type: text/html\r\n\r\n"%>

<html><body>
<form method=post enctype=application/x-www-form-urlencoded>
<hr><h1>A Form: </h1>
<textarea cols =50 name=get_text rows=5></textarea>
<br><input type=submit></form>
<% require 'cgi'%>
<%cgi = CGI.new%>
<%content=cgi['get_text']%>
<%if content =~ /tk\s*test/%>
<%require 'tk'%>
<% message = TkVariable.new%>
<% "padx 20; pady 20"%>
<% txt=TkText.new.pack%>
<% txt.insert('end', content)%>
<% TkLabel.new {%>
<%text 'Enter a message:'%>
<%pack {ph}%>
<%}%>
<% entry = TkEntry.new{%>
<%textvariable message%>
<%}%>
<% entry.pack%>
<% TkButton.new {%>
<%text 'Send'%>
<% command proc {p message.value; exit}%>
<% pack { ph }%>
<%}%>
<%Tk.mainloop%>
<%end%>

</body></html>

Usually <%= aString%> is preferred to <%print aString%>
in an Erb program, since the latter does not always
follow the order of instructions of the program.

However, in this example, when I changed the second
line into <%="Content-type: text/html\r\n\r\n"%>,
a puzzling thing happened. The first page containing
the form is displayed all right. But when the server
sends back a message "All right", Apache produced the
error message '"bad header= "All right"'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top