1

我最近用 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

提前感谢您的帮助。

4

1 回答 1

2

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

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

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

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

require '/var/www/mypages/globalfuntions'

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

于 2012-04-28T22:12:33.930 回答