我正在将Rails 2应用程序迁移到Rails 3,并遇到一个主要问题。我有一个被称为在我application.html.erb称为check_author_role
方法,它是扔Rails 3:application.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/**/"]
我认为这会做到这一点。不过,我仍然收到错误。这意味着,执行下列操作之一是怎么回事:
- 的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
而这里的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个网站。我会测试这个。
并重新启动后,编辑服务器? – rubyprince 2011-06-06 15:07:53
我有一个运行“rails s”的终端窗口,我点击CTRL + C停止它,然后再次输入“rails s”命令。这是足够的,还是有另一个重新开始,我是愚蠢的和遗忘的方面? – isthmus 2011-06-06 15:24:13
您的模块(或类)是否在lib/authenticated_system.rb中正确命名?含义:它是否定义了“模块AuthenticaedSystem”? – theIV 2011-06-06 15:42:26