【发布时间】:2012-12-30 07:02:15
【问题描述】:
我在带有 MongoID gem 的 Rails 应用程序中使用 MongoDB 作为数据库。我想使用 n aafter_create 回调方法从模型中调用辅助方法。这怎么可能?
我的型号代码是:
class Department
include ApplicationHelper
after_create :create_news
private
def create_news
@user = ApplicationHelper.get_current_users
end
end
我的帮助代码是:
module ApplicationHelper
def get_current_users
current_user
end
end
当我创建新部门时,出现以下错误。
undefined method `get_current_users' for ApplicationHelper:Module
如何消除错误?
提前致谢。
【问题讨论】:
-
你为什么要让这个帮手?助手往往用于为您的视图生成标记。这个方法看起来像是属于一个模型。
-
我做这个助手来获取模型中的当前用户信息。
-
然后让它成为你模型中的一个方法?
-
我不能在模型中使用 current_user。所以我尝试了替代
-
我的实际问题是:quora.com/Ruby-on-Rails/…
标签: ruby-on-rails ruby-on-rails-3 model