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?
#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
#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.
– 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!
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.
– 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:
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.
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
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?
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: Peak Obsession
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:
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).
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.