Promise Preston
Promise Preston

Reputation: 28920

Rails: Can't use collection outside resource(s) scope (ArgumentError)

I am working on a Rails 6 application on Ubuntu 20.04.

Right now I am trying to define an import route under the products resources.

Below is my routes file:

Rails.application.routes.draw do
  resources :products 
    collection do
      post :import
    end
end

However, when I start my Rails server, I get the error below:

`collection': can't use collection outside resource(s) scope (ArgumentError)

Still trying to figure out where the issue is.

Upvotes: 0

Views: 169

Answers (1)

Promise Preston
Promise Preston

Reputation: 28920

I found the fix for it.

Here's how I fixed it:

I was supposed to wrap the import route collection in a block under the products resources.

So instead of this:

Rails.application.routes.draw do
  resources :products
    collection do
      post :import
    end
end

It should be this:

Rails.application.routes.draw do
  resources :products do
    collection do
      post :import
    end
  end
end

That's all.

I hope this helps

Upvotes: 1

Related Questions