Apache与Eruby不解析需要正确声明
问题描述:
我最近配置了Apache eruby并且运行了一些rhtml页面。我有一个globalfunctions.rb
文件,我希望可以在网站上运行的所有页面都可用。Apache与Eruby不解析需要正确声明
不过,我有一个问题:把一个需要声明RHTML使得它打破,并返回错误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 %[email protected]<a href="#{target}" class="selected">#{title}</a>@
else
puts %[email protected]<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
谢谢您提前帮助。
答
打印出来$LOAD_PATH
和Dir.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>
你可能会发现,Ruby解释器的当前工作目录(Dir.pwd
)是不一样的RHTML文件的位置。所以Ruby找不到globalfunctions
,因为它只能在$LOAD_PATH
中查找它。
在你需要要求您的文件绝对路径,这样的情况:
require '/var/www/mypages/globalfuntions'
或可选择地将您的globalfuntions.rb
无论是在任何目录$LOAD_PATH
点,或进入地方Dir.pwd
点( Ruby解释器的当前工作目录)。
谢谢你,这个伎俩! – Mark 2012-05-01 19:45:00