【问题标题】:Redmine/Ruby/Rails - Use custom helper methods inside of another custom helperRedmine/Ruby/Rails - 在另一个自定义助手中使用自定义助手方法
【发布时间】:2018-08-10 10:16:03
【问题描述】:

我不了解 Ruby&Rails,但我想构建一个小型自定义 Redmine 插件以满足我的个人需求。 而且我面临的问题对于 Ruby 专家来说可能看起来很简单。 我有两个助手(模块):

  • helper1

  • helper2

我想在helper2.method3 中使用helper1.method1。 我尝试了以下方法来实现这一点:

  • 只需在helper2.method3 内调用方法helper1.method1,并认为关系会自动解决 - 不起作用;

  • helper2 中通过require '../../relative/path' 要求helper1 - 无效;

  • helper2 中通过require '../../relative/path' 要求helper1 - 无效;

  • helper2 中包含helper1 - 无效

我试图查找如何正确地从另一个自定义助手内部的一个自定义助手调用方法的信息,但没有找到任何相关结果。大部分结果是关于如何在视图、控制器、设置视图中调用自定义帮助方法。

那么,有人可以向我解释如何在另一个自定义助手中正确使用方法吗?

最好的,问候。

【问题讨论】:

  • 我认为您可以尝试将两个辅助模块都包含到您的控制器类中(例如github.com/twinslash/redmine_omniauth_google/blob/…)。不确定,但我认为如果在控制器中可以访问包含的帮助器中的方法,那么这些方法可以相互调用。

标签: ruby-on-rails ruby redmine helpers redmine-plugins


【解决方案1】:

对于常用功能,我建议

  1. 在 lib 文件夹中创建一个模块(例如lib/my_plugin/common_code.rb
  2. 在插件 init.rb 中需要它:

    ActionDispatch::Callbacks.to_prepare do 
      require 'my_plugin/common_code'
    end
    
  3. 将它包含在每个需要的助手中

    include MyPlugin::CommonCode
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多