Consider this CGI script:
#!ruby
require “cgi”
cgi = CGI.new(“html4”)
cgi.out {
CGI.pretty(
cgi.html {
cgi.head { cgi.title{“Example”}+"<link rel=“stylesheet”
type=“text/css” href=“example.css”>" } +
cgi.body{“This should have a green background”}
}
)
}
with this example.css, both residing in the same directory:
body {
background-color: #00ff00;
}
it produces this html output:
Example
This should have a green background
Perfectly fine, it seems. The only problem is, that the background isn’t
green, it’s white (Firefox’ default color, on my system).
However, if I save the html-source to a static example.html, put in the
same directory as my example.rb and open it in the browser, the css is
correctly applied and the background turns green.
It works if I put the css directly in the example.rb like this:
#!ruby
require “cgi”
cgi = CGI.new(“html4”)
cgi.out {
CGI.pretty(
cgi.html {
cgi.head {cgi.title{“Example”}+
cgi.style(‘type’=>‘text/css’){File.read("./example.css")}} +
cgi.body{"This should have a green background"}
}
)
}
But this way the css is not cacheable…
Where lies the problem in my script?
(FYI I use ruby 1.8.2 (2004-12-25) [i386-mswin32], Apache and Windows XP
Pro)
Peter Seidel wrote:
cgi.body{"This should have a green background"}
it produces this html output:
This should have a green background
Is this HTML (or the script that generates it) located in the same
directory
as your CSS example file? If not, it won’t work. When there is no path
given in “href”, just a filename, the target file must reside in the
same
directory as the source file, whether that is a CGI script or a Web
page.
You don’t show how you are making the generated HTML available to the
browser. It (or its generating script) must be located in the same
directory as the example CSS, or a path must be provided as part of the
“href” address.
Perfectly fine, it seems. The only problem is, that the background isn’t
green, it’s white (Firefox’ default color, on my system).
However, if I save the html-source to a static example.html, put in the
same directory as my example.rb and open it in the browser, the css is
correctly applied and the background turns green.
And that is the only way this example would work. The script that
generates
the HTML must be located in some specific way with regard to the CSS
file,
in order for the CSS files to be located by the browser.
It works if I put the css directly in the example.rb like this:
No need for that.
/…
Where lies the problem in my script?
The problem is not your script per se, it is in the part you don’t show
how you locate it with respect to the CSS file.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160
The problem is not your script per se, it is in the part you don’t
show – how you locate it with respect to the CSS file.
Both files are in /cgi-bin/
http://127.0.0.1/cgi-bin/example.rb
http://127.0.0.1/cgi-bin/example.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFBoRC11V8mqIQMRsRA4y1AJ4lm+L66DgeXtaL6TAiXoC+RLYbOQCfflRM
QfjrzUGihUM3d0KSHFGOzCI=
=AZRF
-----END PGP SIGNATURE-----
Peter Seidel wrote:
http://127.0.0.1/cgi-bin/example.html
Okay. I will try again.
Where is the example.css file located? Is it located in the same
directory
as the example.rb script, or not?
IF the example.css file is located in the same directory as the
example.rb
CGI script, and assuming the script is really emitting the HTML you
posted
(check using “view source”), the browser will read it correctly.
IF, by contrast, the example.css file is NOT located in the same
directory
as the example.rb CGI script, the browser will NOT be able to find it.
So. Where is the example.css file located?
You know, you can always create an address for the CSS file that
unambiguously identifies its location. Let’s say it is located on your
server at /styles/example.css. Then you can say:
This absolute addressing style is commonly used to avoid the kinds of
problems you are having.
Peter Seidel wrote:
Sorry, my language was ambiguous. With “both files” I meant the script
and the css file. In fact, all the files are in /cgi-bin/.
I just put example.rbx and example.css in ~/ruby on my server and it
works. The page has a green background.
I did have a problem with browser cache, though… I had tried it
before example.css existed, and it was a white background. Making
example.css and refreshing several times didn’t fix the problem. I had
to tell it to do a full page reload (ctrl-click reload) before it showed
the green background.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160
Sorry, my language was ambiguous. With “both files” I meant the script
and the css file. In fact, all the files are in /cgi-bin/.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFBokk11V8mqIQMRsRA7hPAJ0bHbk3WyiS0IMIW0iuCI9TsEMSogCffYp7
anjXtV36eFNifY0ddRH1yfo=
=grMT
-----END PGP SIGNATURE-----
When in doubt about browser caching you might want to add version
parameter to the URL, ie:
cgi.head { '<link rel="stylesheet" type="text/css"
href=“example.css?v=’ + Time.now.to_i.to_s + '”>’ } … etc.
This works for external JavaScript files as well.
Mike D.
http://www.rubywizards.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160
I’ve found the problem. It seems that Apache does not serve the file
correctly if it’s located in the /cgi-bin/ directory. If I move it to
/htdocs and adapt the script it works fine.
Thank you for your kind replies.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFBxe/11V8mqIQMRsRA0okAJ9K9EJHa2coYEXeSqG4ybKDQ3GOYgCeL0TJ
P09WYuLRmYjE0wfEZrLXi5Q=
=Jufx
-----END PGP SIGNATURE-----