2011-06-06 27 views
2

我正在将Rails 2应用程序迁移到Rails 3,并遇到一个主要问题。我有一个被称为在我application.html.erb称为check_author_role方法,它是扔Rails 3:a​​pplication.rb不加载?

undefined local variable or method `check_author_role' 

的check_author_role方法是在一个名为LIB/authenticated_system.rb定义。

我了解到,Rails 3中不再自动加载的lib/目录,所以我增加了以下行config/application.rb

config.autoload_paths += %W(#{config.root}/lib) 
config.autoload_paths += Dir["#{config.root}/lib/**/"] 

我认为这会做到这一点。不过,我仍然收到错误。这意味着,执行下列操作之一是怎么回事:

  1. 的config/application.rb中没有被正确加载
  2. 我的自动加载语法是错误的
  3. 我定义在一个过时的方式
  4. 方法
  5. 我打电话我已经在这几个小时,现在已经被废弃的方法

的方法,不能使头或它的尾巴。 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 

而这里的app/layout/application.html.erb样子:

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

最后,这里的config/application.rb

require File.expand_path('../boot', __FILE__) 

require 'rails/all' 

Bundler.require(:default, Rails.env) if defined?(Bundler) 

module MyApp 
    class Application < Rails::Application 
    # Settings in config/environments/* take precedence over those specified here. 
    # Application configuration should go into files in config/initializers 
    # -- all .rb files in that directory are automatically loaded. 

    # Custom directories with classes and modules you want to be autoloadable. 
    # config.autoload_paths += %W(#{config.root}/extras) 
    config.autoload_paths += %W(#{config.root}/lib) 
    config.autoload_paths += Dir["#{config.root}/lib/**/"] 

    ... 

    end 
end 

我承认我很模糊关于辅助方法如何工作,尤其是在Rails中这是我注意到的。

lib/authenticated_system.rb

# Inclusion hook to make methods 
# available as ActionView helper methods. 
def self.included(base) 
    base.send :helper_method, :current_user, :logged_in?, :check_role, :check_administrator_role, :check_author_role, :has_role, :has_administrator_role, :has_author_role 
end 

我会诚实地说我真的不知道是什么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。它几乎是空的,只有三条线。

class ApplicationController < ActionController::Base 
    protect_from_forgery 
end 

我的假设:app/controllers/application_controller.rb是新的Rails 3档,而app/controllers/application.rb我的旧代码从我的Rails 2个网站。我会测试这个。

+0

并重新启动后,编辑服务器? – rubyprince 2011-06-06 15:07:53

+0

我有一个运行“rails s”的终端窗口,我点击CTRL + C停止它,然后再次输入“rails s”命令。这是足够的,还是有另一个重新开始,我是愚蠢的和遗忘的方面? – isthmus 2011-06-06 15:24:13

+1

您的模块(或类)是否在lib/authenticated_system.rb中正确命名?含义:它是否定义了“模块AuthenticaedSystem”? – theIV 2011-06-06 15:42:26

回答

0

AuthenticatedSystem模块是否混合到您的application_controller

如果是这样,那么方法将不会自动在视图中可用。

您需要添加类似:

helper :check_author_role 

...你application_controller,该AuthenticatedSystem模块中混合后。

+0

我已将相关代码位添加到原始问题中。我注意到的一件事是,有两个名称相似的文件:一个看起来有我的网站的旧的(Rails 2)代码,另一个是Rails 3正在打的。修复可能解决问题的方法。不幸的是,我对诸如'helper'和'base.send'这些东西的了解很薄弱,所以我不完全清楚这些部分是如何工作和相互交谈的。 – isthmus 2011-06-06 17:43:10

+0

是的,看起来像这是什么使'check_author_role'方法丢失:两个版本的'application_controller'之间的文件名混淆。非常感谢大家! – isthmus 2011-06-06 17:57:16