简体   繁体   English

Rails 3:application.rb 没有加载?

[英]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:这意味着正在发生以下情况之一:

  1. config/application.rb isn't being loaded properly config/application.rb 未正确加载
  2. My autoload syntax is wrong我的自动加载语法错误
  3. I'm defining the method in a deprecated way我正在以不推荐的方式定义该方法
  4. I'm calling the method in a deprecated way我以不推荐的方式调用该方法

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM