Why do I get uninitialized constant Spree::Admin::CheckoutsController (NameError) for my decorator?

时间:2016-04-04 18:28:56

标签: ruby-on-rails ruby metaprogramming spree

I have an existing decorator which looks like this:

app/controllers/products_controller_decorator.rb

  1 module Spree
  2   Admin::ProductsController.class_eval do
  3     def update_stock_location
            # my custom code

^ That works.

But when I'm trying to create a new decorator for the CheckoutsController, I get this error:

app/controllers/checkouts_controller_decorator.rb:2:in 
`<module:Spree>': uninitialized constant Spree::Admin::CheckoutsController (NameError)
        from /Users/martins/Work/SolidusApp/app/controllers/checkouts_controller_decorator.rb:1:in `<top (required)>'
        from /Users/martins/.rvm/gems/ruby-2.2.3@solidus/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
        from /Users/martins/.rvm/gems/ruby-2.2.3@solidus/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
        from /Users/martins/Work/SolidusApp/config/application.rb:24:in `block (2 levels) in <class:Application>'

This is what my Spree::Admin::CheckoutsController looks like: app/controllers/checkouts_controller_decorator.rb

  1 module Spree
  2   Admin::CheckoutsController.class_eval do
  3
  4     def complete
            # my custom code

I don't understand why it get an uninitialized constant Spree::Admin::CheckoutsController (NameError). The files looks identical to me.

This is the original class:

solidus/api/app/controllers/spree/api/checkouts_controller.rb

  1 module Spree
  2   module Api
  3     class CheckoutsController < Spree::Api::BaseController

1 个答案:

答案 0 :(得分:0)

You want Api::CheckoutsController not Admin::CheckoutsController

module Spree
  Api::CheckoutsController.class_eval do
    # ...

Others who find this answer may be looking for Admin::CheckoutController (singular, no 's' after checkout)