Search code examples
ruby-on-railsroutesspreesolidus

Solidus API Routes


Right now I'm looking at the edit product view in the admin interface, but every time I try to find the option types, my api returns a 404. This appears to be because Select2 is hitting the following url: /ecommerce/api/option_types?... (not including the actual query on purpose), even though my api url should be at /store/api/ since I have the following in my routes.rb file: mount Spree::Core::Engine, at: '/store'

I confirmed that when I switch the code to mount Spree::Core::Engine, at: '/ecommerce' temporarily in my routes.rb file, my option types appeared correctly in the select2 select box.

My only guess is that earlier in the project, I had mounted the spree engine at /ecommerce (ie mount Spree::Core::Engine, at: '/ecommerce'). But I have since changed the code to mount it at store, and after that have restarted the rails server in the terminal and made sure to hard refresh the browser (in case there was a caching issue somewhere), but still the select2 form is hitting the wrong route (/ecommerce/api/option_types?...).

I tried digging through the source code, but it gets very dense very quick with select2 and its js and everything, haha.

Any guesses on how to fix this?


Solution

  • At first I was not able to reproduce the issue. But later I figured it out. The issue is caused by rails caching the assets... you can fix the issue simply by removing the content of tmp/cache/assets/ directory.

    Why is the issue happening? The routes used in the backend for JS API calls are defined and stored in the JS object Spree.routes, youn can inspect its contents in the browser javascript console.

    These URLs prefix comes from Spree.pathFor defined in core/app/assets/javascripts/spree.js.erb:

    Spree.mountedAt = function() {
      return "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>";
    };
    

    While Rails.application.routes.url_helpers.spree_path changes when you change the Spree mount path, this JS file, once generated, is not going to change, as its MD5 checksum is still the same. So the cached version in the tmp/cache/assets/ directory is going to be used.