Apache与Eruby没有正确解析require语句

时间:2012-04-28 22:00:29

标签: ruby apache2 require eruby

我最近使用eruby配置了Apache并运行了一些rhtml页面。我有一个globalfunctions.rb文件,我希望该文件可用于我在网站上运行的所有页面。

但是,我遇到了一个问题:在rhtml中放入一个require语句使其中断并返回错误500.这里是该页面的代码:

<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>

全局函数文件:

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

最后,这是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

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在您的rhtml文件中打印$LOAD_PATHDir.pwd

<!-- 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>

您可能会发现Ruby解释器的当前工作目录(Dir.pwd)与您的rhtml文件的位置不同。因此Ruby无法找到globalfunctions,因为它只在$LOAD_PATH中查找。

在这种情况下,您需要使用绝对路径来获取文件,例如:

require '/var/www/mypages/globalfuntions'

或者将globalfuntions.rb放在$LOAD_PATH指向的任何目录中,或放在Dir.pwd所指向的位置(Ruby解释器的当前工作目录)。