0

I want to have an Image controller and allow single endpoint such as:

/images/upload

but disable all the other default ones.

resources :images, only: [] do
 collection do
  post "upload"
 end
end

This is my current approach that does the job, but is it the right one? Is there some sort of :none keyword to disable default routes? Or should i not use resources and do it some other way?

deltakroneker
  • 541
  • 1
  • 4
  • 16

2 Answers2

4

Just use a single post route:

post "/images/upload", to: "images#upload", as: :images_upload
Nicolas Blanco
  • 11,164
  • 7
  • 38
  • 49
1

IMO uploading an image can be understood as creating an image, therefore I would simply use the create method instead of an upload method:

resources :images, only: [:create]
spickermann
  • 100,941
  • 9
  • 101
  • 131
  • I'm trying to follow the advice from this comment https://stackoverflow.com/a/42784598/12238373. I'm not creating an Image object, but rather I'm just uploading an image to Cloudinary, and returning the URL to the client, nothing else. Does it still seem appropriate to remove upload method and assign this code to create method? – deltakroneker Jun 23 '20 at 22:06
  • 1
    Sure. It is a Rails convention that `HTTP POST` requests are handled by the `create` method of the controller. – spickermann Jun 24 '20 at 06:27