简体   繁体   English

在Spree Commerce gem上创建新计算器时出错

[英]Error when create a new Calculator on Spree Commerce gem

I am learning rails and I like to learn by practicing trying to create something real. 我正在学习Rails,并且喜欢通过练习尝试创建真实的东西来学习。

I'm having some problems trying to create a new calculator to be used in the shipping area of ​​spree commerce. 我在尝试创建一个新的计算器以在疯狂商务的运输领域中使用时遇到一些问题。

Following this documentation tutorial. 遵循本文档教程。

http://guides.spreecommerce.org/developer/calculators.html#creating-a-new-calculator http://guides.spreecommerce.org/developer/calculators.html#creating-a-new-calculator

Following the tutorial i created this class in this directory. 遵循本教程,我在此目录中创建了此类。

app/models/spree/calculator/shipping/my_own_calculator.rb

My class is this way 我的课是这样

class MyOwnCalculator < Spree::ShippingCalculator
  def self.description
    Spree.t(:shipping_flat_rate_per_order)
  end

  def compute_package(package)
    self.preferred_amount
  end

  def available?(object)
    object.currency == "USD"
  end
end

and my file config/initializers/spree.rb 和我的文件config/initializers/spree.rb

# Configure Spree Preferences
#
# Note: Initializing preferences available within the Admin will overwrite any changes that were made through the user interface when you restart.
#       If you would like users to be able to update a setting with the Admin it should NOT be set here.
#
# Note: If a preference is set here it will be stored within the cache & database upon initialization.
#       Just removing an entry from this initializer will not make the preference value go away.
#       Instead you must either set a new value or remove entry, clear cache, and remove database entry.
#
# In order to initialize a setting do:
# config.setting_name = 'new value'
Spree.config do |config|
  # Example:
  # Uncomment to stop tracking inventory levels in the application
  # config.track_inventory_levels = false
end

Spree.user_class = "Spree::User"

config = Rails.application.config
config.spree.calculators.shipping_methods << Spree::Calculator::Shipping::MyOwnCalculator

the error is this: 错误是这样的:

Unable to autoload constant Spree::Calculator::Shipping::MyOwnCalculator, expected /home/davi/dev/attsalaoruby/app/models/spree/calculator/shipping/my_own_calculator.rb to define it (LoadError)

and my stack trace is this way 我的堆栈跟踪就是这样

/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:512:in `load_missing_constant': Unable to autoload constant Spree::Calculator::Shipping::MyOwnCalculator, expected /home/davi/dev/attsalaoruby/app/models/spree/calculator/shipping/my_own_calculator.rb to define it (LoadError)
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:203:in `const_missing'
    from /home/davi/dev/attsalaoruby/config/initializers/spree.rb:21:in `<top (required)>'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/engine.rb:648:in `block in load_config_initializer'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/activesupport-5.0.1/lib/active_support/notifications.rb:166:in `instrument'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/engine.rb:647:in `load_config_initializer'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/engine.rb:611:in `each'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/engine.rb:611:in `block in <class:Engine>'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/initializable.rb:30:in `run'
    from /home/davi/.rvm/gems/ruby-2.3.0@5.0.0.1/gems/railties-5.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/davi/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
    from /home/davi/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
....

您是否在模块内部这样定义了您的类: module Spree module Calculator::Shipping class MyOwnCalculator < Spree::ShippingCalculator ... end end end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM