[英]Rails 3: application.rb not loading?
I'm migrating a Rails 2 app over to Rails 3, and hitting a major problem.我正在将 Rails 2 应用程序迁移到 Rails 3,并遇到了一个主要问题。 I've got a method being called in my application.html.erb called
check_author_role
which is throwing我在我的应用程序中调用了一个方法。html.erb 称为
check_author_role
正在抛出
undefined local variable or method `check_author_role'
The check_author_role method is defined in a file called lib/authenticated_system.rb. check_author_role 方法在名为 lib/authenticated_system.rb 的文件中定义。
I learned that Rails 3 no longer autoloads the lib/
directory, so I've added the following lines to config/application.rb
:我了解到 Rails 3 不再自动加载
lib/
目录,所以我在config/application.rb
中添加了以下几行:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
I thought that would do it.我以为这样就可以了。 However I'm still getting the error.
但是我仍然收到错误消息。 This implies that one of the following is going on:
这意味着正在发生以下情况之一:
I've been at this for a few hours now and can't make heads or tails of it.我已经在这工作了几个小时了,但不能完全理解它。 All was fine before the Rails 3 update.
在 Rails 3 更新之前一切都很好。 Anyone have some suggestions?
有人有什么建议吗?
Here's what lib/authenticated_system.rb
looks like:这是
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
And here's what app/layout/application.html.erb
looks like:这是
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>
And lastly, here's config/application.rb
最后,这里是
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
I admit that I'm fuzzy on how helper methods work, especially in Rails 3. Here's what I'm noticing.我承认我对辅助方法的工作方式很模糊,尤其是在 Rails 3 中。这就是我注意到的。
In lib/authenticated_system.rb
:在
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
I'll be honest and say I don't really know what base.send
is all about.老实说,我真的不知道
base.send
是什么。
In app/controllers/application.rb
I have the following:在
app/controllers/application.rb
我有以下内容:
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
include AuthenticatedSystem
Again, I'm afraid I don't fully understand what exactly this code is up to.同样,恐怕我不完全理解这段代码到底是做什么的。
Curiously I notice that I also have a file in the same directory with a very similar name: app/controllers/application_controller.rb
.奇怪的是,我注意到我在同一目录中还有一个名称非常相似的文件:
app/controllers/application_controller.rb
。 It's almost empty, with only three lines.它几乎是空的,只有三行。
class ApplicationController < ActionController::Base
protect_from_forgery
end
My hypothesis: app/controllers/application_controller.rb
is the new Rails 3 file, while app/controllers/application.rb
has my old code from my Rails 2 site.我的假设:
app/controllers/application_controller.rb
是新的 Rails 3 文件,而app/controllers/application.rb
有我的 Rails 2 站点上的旧代码。 I'll test this.我会测试这个。
Just for reference, yes, app/controllers/application.rb
was renamed to application_controller.rb
in Rails 2.3仅供参考,是的,在 Rails 2.3 中
app/controllers/application.rb
被重命名为application_controller.rb
http://guides.rubyonrails.org/2_3_release_notes.html#application-controller-renamed http://guides.rubyonrails.org/2_3_release_notes.html#application-controller-renamed
Is the AuthenticatedSystem
module mixed in to your application_controller
? AuthenticatedSystem
模块是否混入了您的application_controller
?
If so, then methods there will not be automatically available in the views.如果是这样,那么视图中的方法将不会自动可用。
You need to add something like:您需要添加以下内容:
helper :check_author_role
... in your application_controller
, after mixing in the AuthenticatedSystem
module. ...在您的
application_controller
中,混合AuthenticatedSystem
模块后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.