Reputation: 717
I have been having a problem with Rails lately where all the example code I've seen so far for sending data via POST is always routed to this default root URL which automatically calls a #create method in the controller. I wanted to know how do I go about making a form that sends data via POST but to a different method that I define?
In this particular example, I have a widget that needs to allow for file uploads. However, this upload could result in either a creation or a replace, but in either case it is only specific to this widget on the page. There are other widgets also capable of creating things and I need to keep the logic for them separated instead of just routing them all through some create method. How do I go about this?
Upvotes: 1
Views: 177
Reputation: 19031
This is how you make custom POST routes.
In routes,
resources :whatevers do
member do
post 'customizedpost'
end
end
In your whatever controller
def customizedpost
# all the post stuff
end
And in your form for example
= form_for @whatever, url: customizedpost_whatever_path, html: { method: :post } do |f|
Upvotes: 2