Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3:application.rb是否未加载?_Ruby On Rails_Ruby On Rails 3_Upgrade - Fatal编程技术网

Ruby on rails Rails 3:application.rb是否未加载?

Ruby on rails Rails 3:application.rb是否未加载?,ruby-on-rails,ruby-on-rails-3,upgrade,Ruby On Rails,Ruby On Rails 3,Upgrade,我正在将一个Rails 2应用程序迁移到Rails 3,遇到了一个重大问题。我在application.html.erb中调用了一个名为check\u author\u role的方法,它正在抛出 undefined local variable or method `check_author_role' check\u author\u role方法在一个名为lib/authenticated\u system.rb的文件中定义 我了解到Rails 3不再自动加载lib/目录,因此我在con

我正在将一个Rails 2应用程序迁移到Rails 3,遇到了一个重大问题。我在application.html.erb中调用了一个名为check\u author\u role的方法,它正在抛出

undefined local variable or method `check_author_role'
check\u author\u role方法在一个名为lib/authenticated\u system.rb的文件中定义

我了解到Rails 3不再自动加载lib/目录,因此我在config/application.rb中添加了以下行:

我想那就行了。然而,我仍然得到了错误。这意味着以下情况之一正在发生:

config/application.rb未正确加载 我的自动加载语法错误 我正在以一种不推荐的方式定义该方法 我正在以一种不推荐的方式调用该方法 我做这件事已经有几个小时了,弄不清头绪。在Rails 3更新之前,一切都很好。有人有什么建议吗

下面是lib/authenticated_system.rb的样子:

module AuthenticatedSystem
  protected

  def check_author_role
    check_role('author')
  end    

  def check_role(role)
    if logged_in? && @current_user.has_role?(role)
      true
    else
      access_denied
    end
  end
end
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head profile="http://www.w3.org/2005/10/profile">
    ...
  </head>
  <body>
    ...
    <% if check_author_role %>
      ...
    <% end %>
    ...
  </body>
</html>
下面是app/layout/application.html.erb的外观:

module AuthenticatedSystem
  protected

  def check_author_role
    check_role('author')
  end    

  def check_role(role)
    if logged_in? && @current_user.has_role?(role)
      true
    else
      access_denied
    end
  end
end
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head profile="http://www.w3.org/2005/10/profile">
    ...
  </head>
  <body>
    ...
    <% if check_author_role %>
      ...
    <% end %>
    ...
  </body>
</html>
我承认我对helper方法的工作方式不太清楚,尤其是在Rails3中。这是我注意到的

在lib/authenticated_system.rb中:

老实说,我真的不知道base.send是怎么回事

在app/controllers/application.rb中,我有以下内容:

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  include AuthenticatedSystem
我的假设是:app/controllers/application_controller.rb是新的Rails 3文件,而app/controllers/application.rb有我在Rails 2站点上的旧代码。我将对此进行测试。

您的应用程序控制器中是否混合了经过身份验证的系统模块

如果是这样,则视图中的方法将不会自动可用

您需要添加以下内容:

helper :check_author_role
。。。在您的应用程序\u控制器中,混入经过身份验证的系统模块后。

经过身份验证的系统模块是否混入您的应用程序\u控制器中

如果是这样,则视图中的方法将不会自动可用

您需要添加以下内容:

helper :check_author_role
。。。在您的应用程序_controller中,混入经过身份验证的系统模块后。

仅供参考,是的,在Rails 2.3中,app/controllers/application.rb被重命名为application _controller.rb

仅供参考,是的,在Rails 2.3中,app/controllers/application.rb被重命名为application_controller.rb


编辑后是否重新启动了服务器?我有一个运行rails s的终端窗口,我按CTRL+C停止它,然后再次输入rails s命令。这就足够了吗,或者重启的另一个方面是我愚蠢而忘记的吗?您的模块或类是否在lib/authenticated_system.rb中正确命名?意思:它定义了模块AuthenticatedSystem吗?它看起来像模块AuthenticatedSystem。受保护的。def check_author_role,来自app/views/layout/application.html.erb的调用看起来很简单,您在编辑后重新启动了服务器吗?我有一个运行rails s的终端窗口,我按CTRL+C停止它,然后再次输入rails s命令。这就足够了吗,或者重启的另一个方面是我愚蠢而忘记的吗?您的模块或类是否在lib/authenticated_system.rb中正确命名?意思:它定义了模块AuthenticatedSystem吗?它看起来像模块AuthenticatedSystem。受保护的。def check_author_role,来自app/views/layout/application.html.erb的调用看起来就像我在原始问题中添加了相关的代码位。我注意到有两个文件的名称相似:一个似乎是来自我网站的旧Rails 2代码,另一个是Rails 3正在使用的代码。解决这个问题可能会解决问题。不幸的是,我对helper和base.send等方面的知识非常薄弱,因此我不完全清楚这些部分是如何工作的,以及它们之间是如何相互通信的。是的,这似乎是导致check\u author\u role方法丢失的原因:两个版本的application\u controller之间的文件名混淆。非常感谢大家!我已经在原始问题中添加了相关的代码位。我注意到有两个文件的名称相似:一个似乎是来自我网站的旧Rails 2代码,另一个是Rails 3正在使用的代码。解决这个问题可能会解决问题。不幸的是,我对helper和base.send等方面的知识非常薄弱,因此我不完全清楚这些部分是如何工作的,以及它们之间是如何相互通信的。是的,这似乎是导致check\u author\u role方法丢失的原因:两个版本的application\u controller之间的文件名混淆。非常感谢大家!
helper :check_author_role