grg-n-sox
grg-n-sox

Reputation: 717

How do I make a POST call in Rails without automatically going to the default create method?

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

Answers (1)

Jason Kim
Jason Kim

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

Related Questions