问题
I am new in rails. I'm trying to build Spree extension to use Braintree's drop-in UI. I'm trying to add new braintree.js to my extension. These are the steps I did so far.
Added
braintree.js
to<my_extension>/app/assets/javascripts/spree/frontend/
Added
//= require spree/frontend/braintree
toapp/assets/javascripts/spree/frontend/<my_extension>.js
.Created
braintree.rb
in<my_extension>/config/initializers/
. Content of this file:Braintree::Configuration.environment = :sandbox Braintree::Configuration.merchant_id = "merchant_id" Braintree::Configuration.public_key = "public_key" Braintree::Configuration.private_key = "private_key"
From my spree store I ran
bundle install
, which was successful.Ran
rails g <my_extension>:install
which gives me the following error:append vendor/assets/javascripts/spree/frontend/all.js append vendor/assets/javascripts/spree/backend/all.js insert vendor/assets/stylesheets/spree/frontend/all.css insert vendor/assets/stylesheets/spree/backend/all.css run bundle exec rake railties:install:migrations FROM=spree_hello_ext from "." Would you like to run the migrations now? [Y/n] y run bundle exec rake db:migrate from "." rake aborted! NameError: uninitialized constant Braintree /Users/<user>/Sites/spree_hello_ext/config/initializers/braintree.rb:1:in `<top (required)>' /Users/<user>/Sites/hellostore/config/environment.rb:5:in `<top (required)>' Tasks: TOP => db:migrate => environment (See full trace by running task with --trace)
回答1:
You need to require the braintree gem in your gemspec as a dependency, and then require it lib/spree_hello_ext.rb for it to be initialized before your initializer tries to use it. Similar to how spree_core it required by spree_gateway: https://github.com/spree/spree_gateway/blob/master/lib/spree_gateway.rb#L1
来源:https://stackoverflow.com/questions/30653965/i-would-like-to-add-new-javascript-file-to-my-spree-extension