LoadError - 期望定义。调用模块时Rails错误

时间:2011-12-27 14:45:23

标签: ruby ruby-on-rails-3 module

我有一个名为EntityTrackerHelper的模块。这是代码:

module EntityTrackerHelper
    def self.createUserAction(user_id, type, entity_id)
        existingua = UserAction.find(:first, :conditions=> ["user_id = ? and type = ? and entity_id=?", user_id, type, entity_id])
        if existingua.nil?
            ua = UserAction.new
            ua.user_id = user_id
            ua.type = type
            ua.entity_id = entity_id
            ua.date = Time.now
            ua.save
        else
            existingua.date = Time.now
            existingua.save
        end
    end
end

它用于跟踪实体中的更改和用户访问。 它在控制器中使用如下。

require "#{Rails.root}/lib/EntityTrackerHelper"
class LessonSectionsController < InheritedResources::Base
    def index
      user_id = params[:user_id]
      lesson_id = params[:lesson_id]
      EntityTrackerHelper::createUserAction(user_id, 'LESSON', lesson_id)
      lessonSections = LessonSection.find(:all, :conditions => { :lesson_id => params[:lesson_id] })
      render :json => {:sections => lessonSections.as_json({:only => [:lesson_id,:id,:name]}), :error => ''}
    end
end

我收到以下错误:

LoadError (Expected /<ProjPath>/<ProjName>/app/models/lesson.rb to define LESSON):
  lib/EntityTrackerHelper.rb:12:in `createUserAction'
  app/controllers/lesson_sections_controller.rb:9:in `index'

EntityTrackerHelper中的第12行是UserAction.find...

有什么想法吗?

由于

2 个答案:

答案 0 :(得分:3)

ActiveRecord将使用字段type进行“单表继承”。请参阅http://api.rubyonrails.org/classes/ActiveRecord/Base.html(副标题:单表继承)。

这意味着当它加载UserAction类型LESSON时,ActiveRecord将尝试实例化未定义的类LESSON

您可能应该为type列使用其他名称。

答案 1 :(得分:-1)

您可以尝试使用

**include EntityTrackerHelper**

了解更多信息,请查看此链接Call Module function from Controller (NoMethodError)