Rails Lib 文件加载错误

发布于 2024-10-06 17:19:05 字数 2150 浏览 3 评论 0原文

在我的项目中,我使用一些自定义液体标签,并在“lib”目录中创建了一个名为“Liquid”的文件夹,其中有一个名为 helpers 的液体模块,

module  Liquid::Helpers

end

因此项目树如下所示

Root

--> lib 

      --> Liquid

我正在尝试在我的environment.rb文件中注册我的标签

Liquid::Template.register_filter(Liquid::Helpers)

要在服务器启动时加载我的lib文件,我在application.rb中尝试这个

config.autoload_paths <<  File.join(Rails.root.to_s, "lib")

但我收到以下错误

':未初始化的常量Liquid :: Helpers(NameError)

是什么我在这里失踪了,有人可以帮助我吗,提前谢谢

sameera

更新

-

我在Rails3和Ubuntu上。以下是我的完整错误代码

`<top (required)>': uninitialized constant Liquid::Helpers (NameError)
    from <project path>/config.ru:3:in `require'
    from <project path>/config.ru:3:in `block in <main>'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from <project path>/config.ru:1:in `new'
    from <project path>/config.ru:1:in `<main>'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands/server.rb:65:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:27:in `tap'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

In my project I'm using some custom liquid tags and I have created a folder called 'Liquid' inside my 'lib' directory and there I have a liquid module called helpers,

module  Liquid::Helpers

end

So the project tree is as follows

Root

--> lib 

      --> Liquid

And I'm trying to register my tags in my environment.rb file

Liquid::Template.register_filter(Liquid::Helpers)

To load my lib files when the server starts, I'm trying this in application.rb

config.autoload_paths <<  File.join(Rails.root.to_s, "lib")

But I'm getting the following error

': uninitialized constant Liquid::Helpers (NameError)

What am i missing here, can someone help me, thanks in advance

cheers

sameera

Update -

I'm on Rails3 with Ubuntu. Following is my full error code

`<top (required)>': uninitialized constant Liquid::Helpers (NameError)
    from <project path>/config.ru:3:in `require'
    from <project path>/config.ru:3:in `block in <main>'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from <project path>/config.ru:1:in `new'
    from <project path>/config.ru:1:in `<main>'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands/server.rb:65:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:27:in `tap'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-10-13 17:19:05

我的水平还不够高,无法提出澄清问题,但我的第一条建议是通过以下方式澄清您的问题(考虑在任何地方发布任何问题的可靠建议):

  • 列出 Rails 的版本, Liquid、Gems、Ruby 等您正在使用的版本(尤其是 Rails 版本)

  • 复制粘贴出现错误消息的终端的整个输出。虽然您可能不理解该输出,但对其他人调试您的情况很有用。从终端复制粘贴(包括您运行的命令)会留下零问题

  • 提供有关文件结构的更好信息。终端复制粘贴再次完美。从 Rails 根目录中,您可以运行命令 find lib (除非您使用的是 Windows,在这种情况下您无法使用 find,并且是时候考虑切换到 OSX 或 ubuntu)。

这是一个终端复制粘贴的示例,其中包含所有这些信息:

dhcp-c-200:foo luke$ rails -v && gem -v && ruby -v && gem list | grep liquid
Rails 2.3.5
1.3.5
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
dhcp-c-200:foo luke$ find lib
lib
lib/Liquid
lib/Liquid/some_module_file.rb
lib/tasks
dhcp-c-200:foo luke$ script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant Liquid::Template (NameError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
    from /Users/luke/Sites/foo/config/environment.rb:43
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:84
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from script/server:3
dhcp-c-200:foo luke$ cat lib/Liquid/some_module_file.rb 
module Liquid

end

module Liquid::Foo

enddhcp-c-200:foo luke$ 

...全部在一个终端中。

I'm not a high enough level yet to ask clarifying questions, but my first piece of advice is to clarify your question in the following way (consider this solid advice for posting any question anywhere):

  • list the versions of rails, liquid, gems, ruby, etc that you are using (especially the rails version)

  • copy-paste the entire output of your terminal where the error message is arising. While you may not understand that output, it is useful for someone else to debug your situation. Copy-pasting from the terminal (including the command you ran) leaves zero question

  • provide better info on your file structure. Once again a terminal copy-paste is perfect. From your rails root, you could run the command find lib (unless you're on Windows, in which case you can't use find and it's time to consider switching to OSX or ubuntu).

Here's an example of a terminal copy-paste that includes all that information:

dhcp-c-200:foo luke$ rails -v && gem -v && ruby -v && gem list | grep liquid
Rails 2.3.5
1.3.5
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
dhcp-c-200:foo luke$ find lib
lib
lib/Liquid
lib/Liquid/some_module_file.rb
lib/tasks
dhcp-c-200:foo luke$ script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant Liquid::Template (NameError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
    from /Users/luke/Sites/foo/config/environment.rb:43
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:84
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from script/server:3
dhcp-c-200:foo luke$ cat lib/Liquid/some_module_file.rb 
module Liquid

end

module Liquid::Foo

enddhcp-c-200:foo luke$ 

... all in one terminal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文