cgi scripts and external css

Discussion in 'Ruby' started by Peter Seidel, Sep 12, 2006.

  1. Peter Seidel

    Peter Seidel Guest

    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:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <HTML>
    <HEAD>
    <TITLE>
    Example
    </TITLE>
    <link rel="stylesheet" type="text/css" href="example.css">
    </HEAD>
    <BODY>
    This should have a green background
    </BODY>
    </HTML>

    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, Sep 12, 2006
    #1
    1. Advertisements

  2. Peter Seidel

    Peter Seidel Guest

    Peter Seidel, Sep 12, 2006
    #2
    1. Advertisements

  3. Peter Seidel

    Peter Seidel Guest

    -----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-----
     
    Peter Seidel, Sep 12, 2006
    #3
  4. 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.
     
    William Crawford, Sep 12, 2006
    #4
  5. Peter Seidel

    Mike Dvorkin Guest

    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 Dvorkin
    http://www.rubywizards.com
     
    Mike Dvorkin, Sep 12, 2006
    #5
  6. Peter Seidel

    Peter Seidel Guest

    -----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-----
     
    Peter Seidel, Sep 12, 2006
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.