drewwyatt
drewwyatt

Reputation: 6027

How can I redirect to a Create action?

Ruby Version: 2.0.0

Rails Version: 4.0.1

I have a controller to handle the Paypal Express Checkout workflow. When paypal reports a successful payment, I want to go ahead and create an order. This is my current attempt:

def do_express_checkout
    ##
    # Paypal logic ommited to keep this from being lengthy....
    ##
    @do_express_checkout_payment_response = api.do_express_checkout_payment(@do_express_checkout_payment) if request.post?
    if @do_express_checkout_payment_response.success?
        redirect_to controller: :orders, action: :create, method: :post
    end
end

The routing is the default rails routing:

resources :orders

Right now, this is just redirecting to /orders (the index method). What should I do?

Upvotes: 0

Views: 3407

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53018

You cannot redirect to a POST action. redirect_to works only for GET requests. With resources :orders , routes for create and index look exactly same except the HTTP verb :

 orders GET    /orders(.:format)                      orders#index
        POST   /orders(.:format)                      orders#create

When you do

redirect_to controller: :orders, action: :create, method: :post

it generates a GET request for /orders which goes to index action. method is not a valid option for redirect_to so it is ignored.

An ugly workaround would be, to create a duplicate route for create action with a GET verb:

  resources :orders
  get "create", to: "orders#create", as: :create_order

This will create a route as:

 create_order GET    /create(.:format)                      orders#create

Then you can update do_express_checkout method as below:

if @do_express_checkout_payment_response.success?
    redirect_to create_order_path
end

Upvotes: 3

Related Questions