CGI Module in Rails


#1

I’m trying to use the CGI module to generate the html in a view but am
having trouble. My controller looks like:

class MyTestController < ApplicationController
def cgitest
require ‘cgi’
@cgi=CGI.new(“html3”)
end
end

My cgitest.rhtml view document simply has:

<%= @cgi.out{@cgi.html{@cgi.head{}}} %>

Loading MyTest/cgitest, Rails tells me that ‘html’ is an invalid or
missing method. Why? When I perform the same sequence in irb, I get a
skeleton html document as expected.

I am very new to Ruby and RoR. I’ve used Perl’s CGI module for a very
long time and it looks like Ruby’s is very similar. Does it make any
sense to even try this? Will Ruby purists laugh me out of the member’s
lounge? :slight_smile:

Kirk


#2

On Fri, 16 Dec 2005, Kirk Bocek wrote:

My cgitest.rhtml view document simply has:

<%= @cgi.out{@cgi.html{@cgi.head{}}} %>

try

<%= @cgi.html{ @cgi.head{} } %>

#out sends the headers and returns nil - you need a string.

hth.

-a

===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| – bodhicaryavatara


#3

unknown wrote:

try

<%= @cgi.html{ @cgi.head{} } %>

#out sends the headers and returns nil - you need a string.

Thanks for the reply, hth, but nope: Same error-- “undefined method
`html’ for #CGI:0x2aaaac53fdb0

If I pop “cgi.methods” into the view, the list I get back does not
include ‘html’ or any of the methods in CGI::HtmlExtension. I can’t
figure out why. They are there when I execute the same instructions in
irb, but are missing under rails.

Kirk


#4

I’m so new to RoR that I’m open to answers like:

– This is not how we do things in RoR.
– How could you even think this?
– RTFM (just give me a hint-- I’ve been reading everything)
– Let the scribes strike his name from the records and his family
turned out into the desert!

Kirk :slight_smile:


#5

Zach D. wrote:

I know this doesn’t solve your issue, but I hope it helps give some
understanding to what might be causing it.

Zach

Yes, that helps my understanding. Thanks. I’m not quite up to mucking
about in Rails internals yet. Yes, this was under Webrick. I’ll get it
installed in Apache and see what happens.

Kirk


#6

Kirk Bocek wrote:

I’m so new to RoR that I’m open to answers like:

– This is not how we do things in RoR.
– How could you even think this?
– RTFM (just give me a hint-- I’ve been reading everything)
– Let the scribes strike his name from the records and his family
turned out into the desert!

It appears the CGI::HtmlExtension isn’t getting loaded. Are you doing
your test in Webrick? If so Rails overrides the CGI#initialize method
for Webrick and it doesn’t call super.

It’s in the railties/lib/webrick_server.rb file. The code from that file
from rails trunk is:

— code start —
def initialize(type = “query”, table = nil, stdin = nil)
@env_table, @stdin = table, stdin

 if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
   Apache.request.setup_cgi_env
 end

 extend QueryExtension
 @multipart = false
 if defined?(CGI_PARAMS)
   warn "do not use CGI_PARAMS and CGI_COOKIES"
   @params = CGI_PARAMS.dup
   @cookies = CGI_COOKIES.dup
 else
   initialize_query()  # set @params, @cookies
 end
 @output_cookies = nil
 @output_hidden = nil

end
end
— code end —

So your “html3” argument is getting treated as the type, and you can see
nothing seems to use the type! Perhaps you should try this with Apache
and see if you get the same behavior.

I know this doesn’t solve your issue, but I hope it helps give some
understanding to what might be causing it.

Zach


#7

Kirk Bocek wrote:

I’ll get it
installed in Apache and see what happens.

Yep, that did it. The CGI::HtmlExtension methods work as expected under
Apache. I haven’t installed FastCGI, but this is for testing anyways.

So is webrick broken then?

Kirk


#8

Kirk Bocek wrote:

I’m trying to use the CGI module to generate the html in a view but am
having trouble. My controller looks like:

class MyTestController < ApplicationController
def cgitest
require ‘cgi’
@cgi=CGI.new(“html3”)
end
end

My cgitest.rhtml view document simply has:

<%= @cgi.out{@cgi.html{@cgi.head{}}} %>

I don’t know why this fails, but:

This is not how we do things in RoR.

Most people write HTML code directly and/or with helper methods like
text_field. If you prefer to build (X)HTML cgi.rb-style, you might be
interested in “Builder” templates:
http://wiki.rubyonrails.com/rails/pages/HowtoGenerateXml


#9

Andreas S. wrote:

Most people write HTML code directly and/or with helper methods like
text_field. If you prefer to build (X)HTML cgi.rb-style, you might be
interested in “Builder” templates:
http://wiki.rubyonrails.com/rails/pages/HowtoGenerateXml

Well, Andreas, I read this page and have no idea what it is saying.
I’ve been writing web widgets forever using Perl and the Perl CGI
module. Hence my questions here about the Ruby CGI module. Guess I’ll
start at the builder.rubyforge.org link and see if I can figure things
out.

Are people using any html editors such as Dreamweaver to create views?

Thanks for the pointer.

Kirk


#10

Kirk Bocek wrote:

Andreas S. wrote:

Most people write HTML code directly and/or with helper methods like
text_field. If you prefer to build (X)HTML cgi.rb-style, you might be
interested in “Builder” templates:
http://wiki.rubyonrails.com/rails/pages/HowtoGenerateXml

Well, Andreas, I read this page and have no idea what it is saying.
I’ve been writing web widgets forever using Perl and the Perl CGI
module. Hence my questions here about the Ruby CGI module. Guess I’ll
start at the builder.rubyforge.org link and see if I can figure things
out.

Are people using any html editors such as Dreamweaver to create views?

Thanks for the pointer.

Well, a simple example for your MyTestController is to
create a file named app/views/layouts/my_test.rxml. The
code should look something like:

xml = Builder.new

xml.instruct! # <?xml …>
xml.declare! ‘DOCTYPE’, :html, #
:PUBLIC,
'-//W3C//… ',
‘…’
xml.html { #
xml.head { #
xml.title ‘My test page’ # My test
page
} #
xml.body { #
xml.comment ‘Test comment’ #
xml.em ‘TEST!’ # TEST!
} #
} #

So xml is the builder object, any method you call on it
(except a few special ones appended with a !) just translates
to an XML tag by the same name (xml.head becomes ). If
you supply parameters to the method, they define the content
of the tag. If you give a block to a method, anything inside
the block becomes nested within that tag (as you see with the
xml.html above).

Kirk

E


#11

Eero S. wrote:

Well, a simple example for your MyTestController is to
create a file named app/views/layouts/my_test.rxml. The
code should look something like:

Okay, so with builder we’re back to straight Ruby code without the <% %>
tags. Makes sense. Functionally, it looks pretty similar to CGI. I will
take a look at Builder in more depth. Thanks.

Kirk


#12

Eero S. wrote:

The code should look something like:

Eero, from my ‘Rails Beginner’ viewpoint, there were a few things I
needed to do to get your example to work.

  1. Install Builder using Gem: ‘gem install builder’
  2. Load builder in my controller:
    require ‘rubygems’
    require_gem ‘builder’
  3. Change the first line of your example to ‘xml =
    Builder::XmlMarkup.new’

Now it’s time to figure out the ‘no styles’ message my browser
generates…

Thanks again.
Kirk