且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 ruby​​ on rails 中创建自定义 html 助手

更新时间:2023-09-28 14:45:46

要创建一个新的助手:

  1. 为帮助文件选择一个名称,例如 tags_helper.rb
  2. 在/app/helpers 目录中创建文件
  3. 根据文件名创建模块.在这种情况下

  1. choose a name for the helper file, for instance tags_helper.rb
  2. create the file in the /app/helpers directory
  3. create a module according to the file name. In this case

module TagsHelper
end

  • 将您的助手定义为方法

  • define your helper as method

    module TagsHelper
      def hello_world(name)
        "hello #{name}"
      end
    end
    

  • 现在您可以在视图中使用 hello_world 辅助方法.

    Now you can use the hello_world helper method in your view.