【问题标题】:Apache with Eruby not parsing require statement properly带有 Eruby 的 Apache 无法正确解析 require 语句
【发布时间】:2012-05-09 05:20:10
【问题描述】:

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

提前感谢您的帮助。

【问题讨论】:

    标签: ruby apache2 require eruby


    【解决方案1】:

    在您的 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 解释器的当前工作目录)中。

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多