自动加载常量SpushHelper时检测到循环依赖关系

时间:2015-10-31 19:13:38

标签: ruby-on-rails ruby-on-rails-4

在我的Rails应用程序中,我有几个控制器,每个控制器都应该向移动设备发送推送通知。为了实现这一点,我使用gem'RPush',当我在每个控制器内部使用init RPush,发送通知等方法时,一切正常。但现在我决定重构整个代码并创建模块,其中将存储所有这些方法。建议我必须在ApplicaitonController中包含我的模块,以使其方法在所有cotrollers中可见,我做到了。现在我遇到了问题Circular dependency detected while autoloading constant SpushHelper

enter image description here enter image description here

我的ApplicationController代码:

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
 protect_from_forgery with: :null_session
 include SPushHelper
 create_push_apps
end

UsersController代码:

 class UsersController < ApplicationController
   before_action :authenticate_root!, only: [:index,:destroy]
   before_action :set_user, only: [:destroy] 

   def index
     @users = User.all
   end

   def destroy
    @user.destroy
    redirect_to users_url 
  end

  private

  def set_user
    @user = User.find(params[:id])
  end

  def user_params
    params.require(:user).permit(:push_token, :uuid, :device_type)
  end
end

和我的SPushHelper代码:

 module SPushHelper

  ANDROID_KEY = "my_android_key_here"

  def create_push_apps
      #create android push app instance
      create_android_push_app
    ...
  end

  def create_android_push_app
    if not(defined? @android_app)   
     @android_app = Rpush::Gcm::App.find_by_name("android_app")
     if @android_app.nil?
       @android_app = Rpush::Gcm::App.new
       @android_app.name = "android_app"
       @android_app.auth_key = ANDROID_KEY  
       @android_app.connections = 1
       @android_app.save!
     end
    end
  end


def send_push_to_android(notification)
  get_users_tokens
  n_android = Rpush::Gcm::Notification.new
  n_android.app = Rpush::Gcm::App.find_by_name("android_app")
  n_android.registration_ids = @users_tokens 
  n_android.data = notification
  n_android.save!
end


def get_users_tokens
  if not(defined?(@users))
    @users = User.all
  else
    if @users.nil?
      @users = User.all
    end
end

if not(defined?(@users_tokens))
  @users_tokens = []
end

@users.each do |u|
    @users_tokens << u.push_token
  end  
 end
end

错误看起来很奇怪,因为我根本没有使用SPushHelper。 我做错了什么?

0 个答案:

没有答案