如何将自定义 Haml 帮助器添加到 Rails 项目?

发布于 2024-12-06 14:06:43 字数 820 浏览 5 评论 0原文

我已经创建了 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 技术交流群。

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

发布评论

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

评论(1

深海夜未眠 2024-12-13 14:06:43

所以,当你有 haml_helper.rb 时,它期望它定义 HamlHelper ....但你想要 Haml::Helper。所以:

/helpers/haml/helpers

当你有命名空间控制器时,这是一样的。 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:

/helpers/haml/helpers

This is the same when you have namespaced controllers. Admin::CustomersController is at app/controllers/admin/customers_controller.rb

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