Question

I recently configured Apache with eruby and got some rhtml pages running. I have a globalfunctions.rb file that I want to be available to all of the pages that I have running on the site.

However, I have a problem: putting a require statement in rhtml makes it break and return error 500. Here's the code for the page:

<html>
<head>
    <title>Home | Quantum Software</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<%
require './globalfunctions.rb'
%>
<div class="contentBox">

</div>
</body>
</html>

And the global functions file:

def get_file_name()
    return File.basename(__FILE__)
end

def new_nav_link( target, title )
    currentFileName = get_file_name()

    if target == currentFileName
        puts %Q@<a href="#{target}" class="selected">#{title}</a>@
    else
        puts %Q@<a href="#{target}">#{title}</a>@
    end
end

And lastly, here's the last few lines of error.log:

[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] :
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] no such file to load -- ./globalfunctions.rb
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103]  (
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] LoadError
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] )
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] --- generated code ---
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "<html>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "<head>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "\\t<title>Home | Quantum Software</title>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "\\t<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\" />\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "</head>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "<body>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103]
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] require "./globalfunctions.rb"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "<div class=\\"contentBox\\">\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "</div>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "</body>\\n"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] print "</html>"
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] ----------------------
[Fri Apr 27 23:22:59 2012] [error] [client 174.252.185.103] Premature end of script headers: eruby
[Fri Apr 27 23:23:24 2012] [error] an unknown filter was not added: includes
[Fri Apr 27 23:23:24 2012] [error] an unknown filter was not added: includes
[Fri Apr 27 23:24:04 2012] [error] an unknown filter was not added: includes
[Fri Apr 27 23:27:03 2012] [error] an unknown filter was not added: includes

Thanks for your help in advance.

Was it helpful?

Solution

Print out $LOAD_PATH and Dir.pwd inside your rhtml file:

<!-- For example like this -->
<p>
  The load path is: <br />
  <%= $LOAD_PATH.join("<br />\n") %>
</p>
<p> 
  The current working directory is: <%= Dir.pwd %>
</p>

You will probably find that the current working directory (Dir.pwd) of the Ruby interpreter is not the same as the location of your rhtml file. So Ruby cannot find globalfunctions because it only looks for it in $LOAD_PATH.

In that case you need to require your file with an absolute path, like:

require '/var/www/mypages/globalfuntions'

OR alternatively place your globalfuntions.rb either in any directory that $LOAD_PATH points to, or into the place that Dir.pwd points to (the current working directory of the Ruby interpreter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top