如何将自定义 Haml 帮助器添加到 Rails 项目?
我已经创建了 Haml 助手并将其作为文件放在路径 app/helpers/haml_helper.rb 中,
module Haml::Helpers
def build_segment(files)
files.each do |f|
if f[:dir] == nil
haml_tag :li do
haml_tag :a, :class=>"file", :href=>f[:name] do
f[:name]
end
end
else
haml_tag :li do
haml_tag :a, :class=>"folder", :href=>f[:name] do
f[:name]
end
end
haml_tag :ul do
build_segment(f[:dir])
end
end
end
end
end
但是随后出现错误:
LoadError in SourceFilesController#index
Expected /home/megas/Work/read_the_code/app/helpers/haml_helper.rb
to define HamlHelper
SourceFilesController#index 是一个将使用此自定义助手的操作。
如何将自定义 haml 助手添加到项目中?
I've created Haml helper and put it as file in path app/helpers/haml_helper.rb
module Haml::Helpers
def build_segment(files)
files.each do |f|
if f[:dir] == nil
haml_tag :li do
haml_tag :a, :class=>"file", :href=>f[:name] do
f[:name]
end
end
else
haml_tag :li do
haml_tag :a, :class=>"folder", :href=>f[:name] do
f[:name]
end
end
haml_tag :ul do
build_segment(f[:dir])
end
end
end
end
end
But then I've got an error:
LoadError in SourceFilesController#index
Expected /home/megas/Work/read_the_code/app/helpers/haml_helper.rb
to define HamlHelper
SourceFilesController#index is an action which going to use this custom helper.
How to add custom haml helper to the project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,当你有 haml_helper.rb 时,它期望它定义 HamlHelper ....但你想要 Haml::Helper。所以:
当你有命名空间控制器时,这是一样的。 Admin::CustomersController 位于 app/controllers/admin/customers_controller.rb
So, when you have haml_helper.rb, it expects it to define HamlHelper .... but you wanted Haml::Helper. So:
This is the same when you have namespaced controllers. Admin::CustomersController is at app/controllers/admin/customers_controller.rb