0

I have a post request:

axios.post("/api/main/products/area_home_products.json"

then in routes.rb :

    namespace :api do

    namespace :dashboard do 
     ....other routes...
    end 

    namespace :main do
      post '/products/area_home_products', to: 'products#area_home'

It was working all fine, then all of a sudden, with no significant changes to the controller action or the js file, I get:

No route matches [GET] "/api/main/products/area_home_products.json"

It is not the first time this happens, and I do not know why it happens nor how to solve it

Doing rake routes | grep area_home_products, gets me;

          api_main_products_area_home_products POST     /api/main/products/area_home_products(.:format)                                          api/main/products#area_home
            api_v1_products_area_home_products POST     /api/v1/products/area_home_products(.:format)                                            api/v1/products#area_home
Gotey
  • 449
  • 4
  • 15
  • 41

1 Answers1

0
axios({
    method: 'post',
    url: '/api/main/products/area_home',
    headers: { 'Access-Control-Allow-Origin': '*', 'Accept': 'application/json',
  })

You can use this method of api call. Also, update your route like this:

namespace :api do
  namespace :main do
    resources :products do
      collection do
        post :area_home
      end
    end
  end
end

This is because either your request headers have problem as you are using .json in url which can mislead it as well as you are not using resourceful routing or your api is not properly configured.

Zain Asif
  • 217
  • 2
  • 13