嵌套助手问题
Nested Helpers Issue
我正在尝试将遗留应用程序从 3 迁移到 4(当然接下来我会转到 5 和 6)。基本上它是一个helper heavy app,它封装了很多关于helpers的逻辑。
我在 rails 4 上加载助手时遇到了一些问题,我想我已经解决了,但我似乎遇到了嵌套助手的问题。我正在使用 traceroute
gem 来验证 routes.rb 上的路由是否正常工作,但我遇到了一些问题。
示例:
uninitialized constant Permissions
/home/app/helpers/permissions/charts_helper.rb:1:in `<top (required)>'
图表助手 (app/helpers/permissions/charts_helper.rb)
module Permissions::ChartsHelper
...
end
在 application.rb
上导入助手:
config.autoload_paths << Rails.root.join('lib')
# config.autoload_paths += Dir["#{config.root}/app/helpers/**/"]
Dir["#{config.root}/app/models/concerns/**/*.rb"].each { |file| require file[0..-4]}
Dir["#{config.root}/app/helpers/**/*.rb"].each { |file| require file[0..-4]}
(我不得不注释掉第二行以支持第四行并且它似乎导入了一些助手但不是全部)
基本上我在寻找:
1.) 在 applications.rb 文件中包含这些助手的最佳方式
2.) 让帮手工作
3.) 任何可以帮助我前进的最佳实践或其他建议。
module Permissions
module ChartsHelper
# ...
end
end
Define (and reopen) namespaced classes and modules using explicit
nesting. Using the scope resolution operator can lead to surprising
constant lookups due to Ruby’s lexical scoping, which depends on the
module nesting at the point of definition.
- the Ruby Style Guide
使用 ::
不是 shorthand 用于显式嵌套,它需要首先加载权限以避免丢失常量错误。
module Permissions::ChartsHelper
end
# NameError (uninitialized constant Permissions)
还设置了错误的模块嵌套:
FOO = "I'm in the global scope"
module Permissions
FOO = "I'm Permissions::FOO"
end
module Permissions::ChartsHelper
puts FOO # I'm in the global scope
puts Module.nesting.inspect # [Permissions::ChartsHelper]
end
在Ruby中只有module
和class
关键字改变了当前的模块嵌套。
相比之下,如果您显式嵌套模块:
FOO = "I'm in the global scope"
module Permissions
FOO = "I'm Permissions::FOO"
end
module Permissions
module ChartsHelper
puts FOO # I'm Permissions::FOO
puts Module.nesting.inspect # [Permissions::ChartsHelper, Permissions]
end
end
我正在尝试将遗留应用程序从 3 迁移到 4(当然接下来我会转到 5 和 6)。基本上它是一个helper heavy app,它封装了很多关于helpers的逻辑。
我在 rails 4 上加载助手时遇到了一些问题,我想我已经解决了,但我似乎遇到了嵌套助手的问题。我正在使用 traceroute
gem 来验证 routes.rb 上的路由是否正常工作,但我遇到了一些问题。
示例:
uninitialized constant Permissions
/home/app/helpers/permissions/charts_helper.rb:1:in `<top (required)>'
图表助手 (app/helpers/permissions/charts_helper.rb)
module Permissions::ChartsHelper
...
end
在 application.rb
上导入助手:
config.autoload_paths << Rails.root.join('lib')
# config.autoload_paths += Dir["#{config.root}/app/helpers/**/"]
Dir["#{config.root}/app/models/concerns/**/*.rb"].each { |file| require file[0..-4]}
Dir["#{config.root}/app/helpers/**/*.rb"].each { |file| require file[0..-4]}
(我不得不注释掉第二行以支持第四行并且它似乎导入了一些助手但不是全部)
基本上我在寻找:
1.) 在 applications.rb 文件中包含这些助手的最佳方式 2.) 让帮手工作 3.) 任何可以帮助我前进的最佳实践或其他建议。
module Permissions
module ChartsHelper
# ...
end
end
Define (and reopen) namespaced classes and modules using explicit nesting. Using the scope resolution operator can lead to surprising constant lookups due to Ruby’s lexical scoping, which depends on the module nesting at the point of definition.
- the Ruby Style Guide
使用 ::
不是 shorthand 用于显式嵌套,它需要首先加载权限以避免丢失常量错误。
module Permissions::ChartsHelper
end
# NameError (uninitialized constant Permissions)
还设置了错误的模块嵌套:
FOO = "I'm in the global scope"
module Permissions
FOO = "I'm Permissions::FOO"
end
module Permissions::ChartsHelper
puts FOO # I'm in the global scope
puts Module.nesting.inspect # [Permissions::ChartsHelper]
end
在Ruby中只有module
和class
关键字改变了当前的模块嵌套。
相比之下,如果您显式嵌套模块:
FOO = "I'm in the global scope"
module Permissions
FOO = "I'm Permissions::FOO"
end
module Permissions
module ChartsHelper
puts FOO # I'm Permissions::FOO
puts Module.nesting.inspect # [Permissions::ChartsHelper, Permissions]
end
end