首页 > 解决方案 > Rails:随机已经初始化的常量 X + 先前定义的 X

问题描述

红宝石:2.5.1,导轨:5.1.6

偶尔,这个错误只出现在开发环境中。并在多个平台上。ruby 是安装在“rbenv”还是“rvm”上。

Started GET "/notifications" for 127.0.0.1 at 2018-08-27 14:51:47 +0200
Processing by NotificationsController#index as */*
Started GET "/dashboard_widgets/my_courses/1" for 127.0.0.1 at 2018-08-27 14:51:47 +0200
Started GET "/dashboard_widgets/appointments/2" for 127.0.0.1 at 2018-08-27 14:51:47 +0200
Started GET "/dashboard_widgets/calendars/4" for 127.0.0.1 at 2018-08-27 14:51:47 +0200
Started GET "/dashboard_widgets/tasks/3" for 127.0.0.1 at 2018-08-27 14:51:47 +0200
  Account Load (1.0ms)  SELECT  `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = '' LIMIT 1
  User Load (61.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`account_id` = 1 AND `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  AccountSetting Load (1.3ms)  SELECT `settings`.* FROM `settings` WHERE `settings`.`target_id` = 1 AND `settings`.`target_type` = 'Account'
/Users/alexander/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:457: warning: already initialized constant DashboardWidgets
/Users/alexander/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:457: warning: previous definition of DashboardWidgets was here
/Users/alexander/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:457: warning: already initialized constant DashboardWidgets
/Users/alexander/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:457: warning: previous definition of DashboardWidgets was here
/Users/alexander/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:457: warning: already initialized constant DashboardWidgets
/Users/alexander/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:457: warning: previous definition of DashboardWidgets was here



LoadError (Unable to autoload constant DashboardWidgets::MyCoursesController, expected /Users/alexander/rails_projects/tutorize-business-solutions/app/controllers/dashboard_widgets/my_courses_controller.rb to define it):

Processing by DashboardWidgets::TasksController#show as HTML
  Parameters: {"id"=>"3"}
LoadError (Unable to autoload constant DashboardWidgets::AppointmentsController, expected /Users/alexander/rails_projects/tutorize-business-solutions/app/controllers/dashboard_widgets/appointments_controller.rb to define it):
LoadError (Unable to autoload constant DashboardWidgets::CalendarsController, expected /Users/alexander/rails_projects/tutorize-business-solutions/app/controllers/dashboard_widgets/calendars_controller.rb to define it):
appsignal (2.3.7) lib/appsignal/rack/rails_instrumentation.rb:17:in `call'

appsignal (2.3.7) lib/appsignal/rack/rails_instrumentation.rb:17:in `call'
   (7.9ms)  SET NAMES utf8mb4,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483

appsignal (2.3.7) lib/appsignal/rack/rails_instrumentation.rb:17:in `call'
  Account Load (21.9ms)  SELECT  `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = '' LIMIT 1
  User Load (2.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`account_id` = 1 AND `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  AccountSetting Load (1.3ms)  SELECT `settings`.* FROM `settings` WHERE `settings`.`target_id` = 1 AND `settings`.`target_type` = 'Account'
  DashboardWidget Load (4.5ms)  SELECT  `dashboard_widgets`.* FROM `dashboard_widgets` WHERE `dashboard_widgets`.`account_id` = 1 AND `dashboard_widgets`.`id` = 3 ORDER BY `dashboard_widgets`.`position` ASC LIMIT 1
  Rendering dashboard_widgets/tasks/show.html.erb
  DashboardWidgetShare Load (3.5ms)  SELECT  `dashboard_widget_shares`.* FROM `dashboard_widget_shares` WHERE `dashboard_widget_shares`.`account_id` = 1 AND `dashboard_widget_shares`.`dashboard_widget_id` = 3 LIMIT 1
  Task Load (1.1ms)  SELECT  `tasks`.* FROM `tasks` WHERE `tasks`.`account_id` = 1 AND `tasks`.`deleted_at` IS NULL AND `tasks`.`user_id` = 1 AND `tasks`.`status` = 'opened' ORDER BY `tasks`.`due_date` DESC LIMIT 10
  Rendered dashboard_widgets/tasks/layouts/_widget.html.erb (39.1ms)
  Rendered dashboard_widgets/tasks/show.html.erb (82.7ms)
Completed 200 OK in 959ms (Views: 116.0ms | ActiveRecord: 51.2ms)

和所有其他控制器的路径my_courses_controller.rb都是正确的并经过双重检查。奇怪的是,一旦第二个请求到来,一切正常。到目前为止,生产环境似乎没有问题。铁轨autoload_path没有加长。

该结构的一些示例代码:

module DashboardWidgets
  class WidgetsController < ::ApplicationController
    private

    def set_user
      # sets user and stuff...
    end
  end
end

module DashboardWidgets
  class MyCoursesController < WidgetsController
    include CustomView
    layout false

    def show
      authorize [:dashboard_widgets, :my_course]
      set_dashboard_widget
      set_user
      search = Searches::LmsSearch.new current_user.learning_course_ids_by_type.merge(search_params)
      @courses = search.search

      render_custom_view
    end

    ...

end

我们搜索了常见的“已经初始化的常量 X”错误和“X 的先前定义”警告数周,现在我完全不知道问题可能仍然存在。

标签: ruby-on-rails-5.1rails-5.1.6

解决方案


推荐阅读