如何从模型中调用辅助方法?

3 model ruby-on-rails ruby-on-rails-3

我在MongoID gem的Rails应用程序中使用MongoDB作为数据库.我想用n aafter_create回调方法从模型中调用helper方法.这怎么可能?
我的型号代码是:

class Department
  include ApplicationHelper
  after_create :create_news
  private
  def create_news
    @user = ApplicationHelper.get_current_users
  end
end
Run Code Online (Sandbox Code Playgroud)

我的助手代码是:

module ApplicationHelper
  def get_current_users
    current_user
  end
end
Run Code Online (Sandbox Code Playgroud)

当我创建新部门时,会发生以下错误.

undefined method `get_current_users' for ApplicationHelper:Module
Run Code Online (Sandbox Code Playgroud)

如何删除错误?
提前致谢.

Can*_*mak 13

我也使用mongoid并一直使用它.虽然不应该是mongoid独有的.

ApplicationController.helpers.my_helper_method
Run Code Online (Sandbox Code Playgroud)


小智 -1

确保模块文件命名正确,这意味着在您的情况下application_helper.rb,它位于帮助程序库中。

您还可以尝试将助手包含在 ApplicationController ( app/controller/application_controller.rb ) 中。