cgi or eruby? and how to get started with erb?

A

anne001

I have succeeded in writing a form in cgi, and receiving the
information with cgi.
It was tough going as at each step I had to find an example to see how
to translate
html into cgi methods.

1. It seems that eruby would be easier, and I suspect ruby on rails is
built on something like that.
is that true?

2. There seem to be several implementations of eruby, what does that
mean? which one to use?
eruby, erb, erubis...

3. What are the relative advantages of cgi and eruby/erb.

4. In any case, I would like to try to build a version of my form in
eruby.
"programming ruby" says erb is included in ruby, p 242. Where can I
find a tutorial on how to use it?

I found surprisingly little info so far. I tried this advice but it
does not work
https://trac.lighttpd.net/trac/wiki/eRuby

#!/usr/bin/eruby
cgi.assign = ( ".rhtml" => "/usr/bin/eruby" )
<% puts Time.now %>

Thank you for your help
 
A

allan.m.miller

Hi,

Some general thoughts -

First, the reason why you've found so little on erb is that it is VERY
simple and limited. There isn't much more than you've found, a single
page describes everything about how to use it! The most difficult part
of using erb is deployment (see below).

I wrote and deployed an application using erb last year. Frankly, I
would say that if you are doing an application that involves limited
generation of dynamic content (e.g., capturing information from one or
two forms, and then dynamically generating response pages), then use
cgi. If you are going to develop a more complex web application, one
that requires complex and substantial control, and especially database
access, use Rails. Erb is fine, but very, very simple and limited. It
may also be difficult to find an ISP that offers erb w/o Rails (I used
Grokthis.net). I was pretty serious about using erb, but now realize
that Rails is the way to go.

Kind regards,

allan
 
A

anne001

Thank you for your response. I will try cgi and rails then.

Could you post an rhtml file that will work with ruby 1.8.2, and let me
know
where to put it etc so I can see that it works on my machine?

I would like to at least know how to get it to work.
 
T

Timothy Goddard

ERB doesn't know anything about the content it works with. You use it
like this:

student.rb
------------------------
require 'erb'

class Student
@@report_template = ERB.new('report.rhtml')

attr_accessor :name, :age, :grades, :comments

def report
return @@report_template.result(binding) # Can access instance
variables of this class
end
end

john = Student.new
john.name = "John"
john.age = 12
john.grades = {:eek:verall => 'C', :math => 'D', :english => 'A'}
john.comments = "John could work a little harder on his nuber skills"

puts john.report
------------------------

report.rhtml
------------------------
<html>
<body>
<h1>Report for <%= @name %></h1>
<p><%= @name %> has done very well this year, getting an <%=
@grades[:eek:verall] %> for overall performance.</p>
<table>
<tr>
<th>Subject</th>
<th>Grade</th>
</tr>
<% @grades.each do |subject, grade| %>
<% next if subject == :eek:verall %>
<tr>
<td><%= subject.to_s %></td>
<td><%= grade %></td>
</tr>
<% end %>
</table>
<h3>Teacher's Comments</h3>
<p><%= @comments %></p>
</body>
</html>
------------------------

That's it. <% %> includes ruby code in the template. <%= %> includes
ruby code and inserts the result into the template.

ERB needs a binding to run, which is basically a context in which to
run. 'binding' will return the binding of the current object. If you
need to access it externally, you can simply create a method to
retrieve this binding.

P.S. This code is untested.
 
A

anne001

Thank you so much for your examples.

I ran the first one with ruby. the output was a little disappointing:
it only said
report.rhtml
I don't know how to get the program to list the report itself. I will
continue to try
to figure that out.

I copied the second code in a file foo.rhtml in a subdirectory of
CGI-Executables
cd /library/webserver/CGI-Executables/basic
where I have been able to run cgi files.

accessing the file foo.rhtml at
http://127.0.0.1/cgi-bin/basic/foo.rhtml
only gave me an error message

[Wed Apr 26 08:33:24 2006] [error] (8)Exec format error: exec of
/Library/WebServer/CGI-Executables/basic/foo.rhtml failed

I took out the lines I had added for eruby and restarted apache
AddType application/x-httpd-eruby .rhtml
Action application/x-httpd-eruby /cgi-bin/eruby
in httpd.conf as per section on eruby in
http://phrogz.net/ProgrammingRuby/web.html

but I don't know if I need to tell apache how to process rhtml files

There seems to be something I don't know about .rhtml files
 
A

anne001

double wamer !!! I got eruby to work.

I copied eruby to library/webserver/CGI-Executables/eruby.cgi
notice that eruby becomes eruby.cgi!
chmod 755 eruby.cgi

added
AddType application/x-httpd-eruby .rhtml
Action application/x-httpd-eruby /cgi-bin/eruby
to /etc/httpd/httpd.conf
restarted with sudo apachectl graceful

in the eruby file folder library/webserver/Documents/erubyfiles
I put a file .htaccess as I had for the erb file folder with the lines
DirectoryIndex index.rhtml index.html index.htm
AddHandler rubypage .rhtml
Action rubypage /cgi-bin/eruby.cgi

then I ran the test <%= "The Eruby .rhtml parser is up and working
fine."%>
from
http://127.0.0.1/erubyfiles/test.rhtml

This is a bit beyond me, so I hope I caught all the steps in case
someone else wants to
try it. Worked for me.

(I tried copying the erb.cgi to eruby.cgi and replacing erb by eruby
but that did not work, so what
ended up working was a combination of eruby installation instruction
from pickaxe, and erb instructions)
 
T

Timothy Goddard

Those weren't two separate examples. They are one example. The first is
the ruby script and the second is the template. Copy them both into the
same directory with the file names given and try it again.

Good luck.
 
A

anne001

I see, Since rhtml does not have code asking it to execute a ruby file,
I assumed I should run ruby
ruby student.rb
but I just get report.rhtml.
erb does not seem to understand that report.rhtml is not a string per
say, but the name of a file of that name in the same directory.

I moved the rhtml file within the student.rb file as per example on
ruby-doc page
wrapped in %{ }
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html

and then ran ruby student.rb, where student.rb is
require 'erb'

class Student

@template = %{
<html>
<body>
<h1>Report for <%= @name %></h1>
<p><%= @name %> has done very well this year, getting an <%=
@grades[:eek:verall] %> for ove\
rall performance.</p>
<table>
<tr>
<th>Subject</th>
<th>Grade</th>
</tr>
<% @grades.each do |subject, grade| %>
<% next if subject == :eek:verall %>
<tr>
<td><%= subject.to_s %></td>
<td><%= grade %></td>
</tr>
<% end %>
</table>
<h3>Teacher's Comments</h3>
<p><%= @comments %></p>
</body>
</html>
}.gsub(/^ /, '')

@@report_template = ERB.new(@template)

attr_accessor :name, :age, :grades, :comments

def report
return @@report_template.result(binding) # Can access instance
variables of this class
end
end

john = Student.new
john.name = "John"
john.age = 12
john.grades = {:eek:verall => 'C', :math => 'D', :english => 'A'}
john.comments = "John could work a little harder on his nuber skills"

puts john.report


It is a bit like mailmerge in Word, take a database info, and create
individualized html code.
I can see how having the format apart from data can be helpful.

thanks for this example.
 
T

Timothy Goddard

Ah yes. Sorry about that. I thought ERB.new took a filename, not a
string. That should be:

@@report_template = ERB.new(File.read("report.rhtml"))
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top