首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Eruby的Apache无法正确解析require语句

带有Eruby的Apache无法正确解析require语句
EN

Stack Overflow用户
提问于 2012-04-29 06:00:29
回答 1查看 184关注 0票数 1

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

但是,我有一个问题:在rhtml中放置一个require语句会使它中断并返回错误500。下面是该页面的代码:

代码语言:javascript
复制
<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>

和全局函数文件:

代码语言:javascript
复制
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的最后几行:

代码语言:javascript
复制
[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

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-29 06:12:34

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

代码语言:javascript
复制
<!-- 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中查找它。

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

代码语言:javascript
复制
require '/var/www/mypages/globalfuntions'

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10367922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档