我试图像这样添加对Spree Admin PromotionsController的操作
应用程序/控制器/礼包/管理/ 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
但是我收到了这个错误:
自动加载常量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
答案 0 :(得分:1)
问题是文件名为app/controllers/spree/admin/promotions_controller.rb
在spree中,无论何时我们在控制器/模型上执行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
尝试更改文件名。它肯定会解决你的问题。
如果您仍然遇到任何问题,请告诉我。