覆盖 Spree 中的控制器 - 自动加载常量时检测到循环依赖
Override Controller in Spree - Circular dependency detected while autoloading constant
我正在尝试像这样向 Spree Admin PromotionsController 添加操作
app/controllers/spree/admin/promotions_controller.rb
Spree::Admin::PromotionsController.class_eval do
def users
params[:q] ||= {}
@search = @promotion.orders.reverse_chronological.ransack(params[:q])
@users_hash = Hash.new(0)
@search.result.each { |order| @users_hash[order.user] += 1 }
@users_hash = Kaminari.paginate_array(@users_hash.to_a).page(params[:page])
end
end
但是我收到了这个错误:
Circular dependency detected while autoloading constant Spree::Admin::PromotionsController
我还尝试完全覆盖控制器:
module Spree
module Admin
class PromotionsController < ResourceController
before_action :load_data
helper 'spree/admin/promotion_rules'
def users
params[:q] ||= {}
@search =
@promotion.orders.reverse_chronological.ransack(params[:q])
@users_hash = Hash.new(0)
@search.result.each { |order| @users_hash[order.user] += 1 }
@users_hash =
Kaminari.paginate_array(@users_hash.to_a).page(params[:page])
end
protected
def location_after_save
spree.edit_admin_promotion_url(@promotion)
end
def load_data
@calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
@promotion_categories = Spree::PromotionCategory.order(:name)
end
def collection
return @collection if defined?(@collection)
params[:q] ||= HashWithIndifferentAccess.new
params[:q][:s] ||= 'id desc'
@collection = super
@search = @collection.ransack(params[:q])
@collection = @search.result(distinct: true)
.includes(promotion_includes)
.page(params[:page])
.per(params[:per_page] ||
Spree::Config[:admin_promotions_per_page])
end
def promotion_includes
[:promotion_actions]
end
end
end
效果很好,但看起来像拐杖
非常感谢任何帮助,谢谢
路线:
Spree::Core::Engine.routes.draw do
Spree::Core::Engine.add_routes do
namespace :admin do
resources :promotions, only: [] do
member do
get 'users'
end
end
end
end
end
问题出在文件名 app/controllers/spree/admin/promotions_controller.rb
每当我们在 controller/model 上执行 class_eval 时,我们都会创建名称为 'original_filenanme_decorator.rb'
的文件
即app/controllers/spree/admin/promotions_controller_decorator.rb
Spree::Admin::PromotionsController.class_eval do
def users
params[:q] ||= {}
@search = @promotion.orders.reverse_chronological.ransack(params[:q])
@users_hash = Hash.new(0)
@search.result.each { |order| @users_hash[order.user] += 1 }
@users_hash = Kaminari.paginate_array(@users_hash.to_a).page(params[:page])
end
end
尝试更改文件名。一定能解决你的问题。
如果您仍然遇到任何问题,请告诉我。
我正在尝试像这样向 Spree Admin PromotionsController 添加操作
app/controllers/spree/admin/promotions_controller.rb
Spree::Admin::PromotionsController.class_eval do
def users
params[:q] ||= {}
@search = @promotion.orders.reverse_chronological.ransack(params[:q])
@users_hash = Hash.new(0)
@search.result.each { |order| @users_hash[order.user] += 1 }
@users_hash = Kaminari.paginate_array(@users_hash.to_a).page(params[:page])
end
end
但是我收到了这个错误:
Circular dependency detected while autoloading constant Spree::Admin::PromotionsController
我还尝试完全覆盖控制器:
module Spree
module Admin
class PromotionsController < ResourceController
before_action :load_data
helper 'spree/admin/promotion_rules'
def users
params[:q] ||= {}
@search =
@promotion.orders.reverse_chronological.ransack(params[:q])
@users_hash = Hash.new(0)
@search.result.each { |order| @users_hash[order.user] += 1 }
@users_hash =
Kaminari.paginate_array(@users_hash.to_a).page(params[:page])
end
protected
def location_after_save
spree.edit_admin_promotion_url(@promotion)
end
def load_data
@calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
@promotion_categories = Spree::PromotionCategory.order(:name)
end
def collection
return @collection if defined?(@collection)
params[:q] ||= HashWithIndifferentAccess.new
params[:q][:s] ||= 'id desc'
@collection = super
@search = @collection.ransack(params[:q])
@collection = @search.result(distinct: true)
.includes(promotion_includes)
.page(params[:page])
.per(params[:per_page] ||
Spree::Config[:admin_promotions_per_page])
end
def promotion_includes
[:promotion_actions]
end
end
end
效果很好,但看起来像拐杖
非常感谢任何帮助,谢谢
路线:
Spree::Core::Engine.routes.draw do
Spree::Core::Engine.add_routes do
namespace :admin do
resources :promotions, only: [] do
member do
get 'users'
end
end
end
end
end
问题出在文件名 app/controllers/spree/admin/promotions_controller.rb
每当我们在 controller/model 上执行 class_eval 时,我们都会创建名称为 'original_filenanme_decorator.rb'
的文件即app/controllers/spree/admin/promotions_controller_decorator.rb
Spree::Admin::PromotionsController.class_eval do
def users
params[:q] ||= {}
@search = @promotion.orders.reverse_chronological.ransack(params[:q])
@users_hash = Hash.new(0)
@search.result.each { |order| @users_hash[order.user] += 1 }
@users_hash = Kaminari.paginate_array(@users_hash.to_a).page(params[:page])
end
end
尝试更改文件名。一定能解决你的问题。
如果您仍然遇到任何问题,请告诉我。