Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 自动加载常量SpushHelper时检测到循环依赖项_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 自动加载常量SpushHelper时检测到循环依赖项

Ruby on rails 自动加载常量SpushHelper时检测到循环依赖项,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,在我的Rails应用程序中,我有几个控制器,每个控制器都应该向移动设备发送推送通知。为了实现这一点,我使用gem“RPush”,当我在每个控制器中使用init RPush、send notifications等方法时,一切都正常。但现在我决定重构整个代码并创建一个模块,所有这些方法都将存储在这个模块中。建议我必须将我的模块包含在ApplicationController中,以使其方法在所有cotroller中可见,我做到了。现在,在自动加载常量SpushHelper时,我遇到了一个循环依赖项检测

在我的Rails应用程序中,我有几个控制器,每个控制器都应该向移动设备发送推送通知。为了实现这一点,我使用gem“RPush”,当我在每个控制器中使用init RPush、send notifications等方法时,一切都正常。但现在我决定重构整个代码并创建一个模块,所有这些方法都将存储在这个模块中。建议我必须将我的模块包含在ApplicationController中,以使其方法在所有cotroller中可见,我做到了。现在,在自动加载常量SpushHelper时,我遇到了一个循环依赖项检测到的问题

我的应用程序控制器代码:

以及我的SPushHelper代码:

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

SPushHelper模块定义的文件名是什么?根据错误消息,它正在尝试查找SpushHelper,而不是SpushHelper,您是否有另一个名为这样的模块?@Broisasze非常感谢!您的评论非常有用。另外,文件名为“spush_helper.rb”,现在我将此文件中的模块名从SPushHelper更改为SPushHelper,循环依赖项不再出现错误:
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
 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
 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