在/ lib目录中访问Rails模块的问题

时间:2012-07-24 22:03:11

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

我在这里发现了许多类似的问题,但没有一个答案解决了我的问题。

在/ user / 2

加载我的页面时出现以下错误
NoMethodError in User_data#data

Showing /Users/Jimmy/Documents/Launchpad Toys/LPT_Repositories/orbit-analytics/app/views/user_data/data.erb where line #108 raised:

undefined method `<' for nil:NilClass

Extracted source (around line #108):

105:            </div>
106:       </div>
107:       
108:       <% if @videos_number < 1 %><p style="font-size: 18px; margin-top: 25px;">This user has no approved videos.</p>
109:       
110:       <% else %>
111:          

此错误是由控制器为视图中调用的任何和所有实例变量返回空值引起的。

这是相关控制器的开头:

class UserDataController < ApplicationController

    require 'analytics_helper'

    include AnalyticsHelper

    def data

        require "date"

        @user_id = params[:user_id]

错误似乎是由于控制器无法找到位于lib / analytics_helper.rb的模块AnalyticsHelper引起的。这是模块的开头:

Module AnalyticsHelper

    # Average analytics calculations for a given set of videos

    def analytics_tracker(video_ids)        
        total_likes, total_views, total_flags, total_days_visible = 0, 0, 0, 0
        total_characters, total_unique_characters, total_recurring_characters, total_custom_characters, total_custom_backgrounds = 0, 0, 0, 0, 0
        total_energy_level, total_emotions, total_scenes, total_duration = 0, 0, 0, 0

我在application.rb中取消注释了自动加载Rails 3中/ lib目录中的文件:

config.autoload_paths += %W(#{config.root}/lib)

我知道这个问题是由于模块无法加载造成的,但我发现的解决方案都没有帮助我解决问题。任何帮助表示赞赏。感谢。

1 个答案:

答案 0 :(得分:2)

module不应在analytics_helper.rb文件的开头大写。尝试:

    module AnalyticsHelper
      # ...
    end