如何从模型中调用辅助方法? [英] How to call helper method from model?

查看:76
本文介绍了如何从模型中调用辅助方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoID gem的Rails应用程序中将MongoDB用作数据库.我想从带有n aafter_create回调方法的模型中调用辅助方法.怎么可能?
我的模型代码是:

I use MongoDB as database in my Rails application with MongoID gem. I want to call helper method from model with n aafter_create callback method. How is it possible?
My model code is:

class Department
  include ApplicationHelper
  after_create :create_news
  private
  def create_news
    @user = ApplicationHelper.get_current_users
  end
end

我的助手代码是:

And my helper code is:

module ApplicationHelper
  def get_current_users
    current_user
  end
end

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

When I create new department then following error occur.

undefined method `get_current_users' for ApplicationHelper:Module

如何清除错误?
预先感谢.

How to remove error?
Thanks in advance.

推荐答案

我也使用蒙古包,并一直使用它.但是,对于蒙古人来说不应该是唯一的.

I also use mongoid and use this all the time. Shouldn't be unique to mongoid though.

ApplicationController.helpers.my_helper_method

这篇关于如何从模型中调用辅助方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆