This is in my routes.rb file:
resources :orders do
member do
get 'confirm'
get 'cancel'
end
end
Of course I wrote the ‘confirm’ and ‘cancel’ actions in my orders_controller.rb:
def confirm
@order = Order.find(params[:id])
#...
end
def cancel
@order = Order.find(params[:id])
#...
end
..and created both “confirm.html.erb” and “cancel.html.erb” files in my “app/views/orders/” folder.
But when I try to access confirm_order_url or cancel_order_url I am always running into this routing error:
No route matches {:action=>"confirm", :controller=>"orders"}
Can’t figure out what’s missing!
Any idea please?
PS: I’m using RoR v.3.1.0
EDIT#1:
It seems the routes are set properly:
$ rake routes
[..]
confirm_order GET /orders/:id/confirm(.:format) {:action=>"confirm", :controller=>"orders"}
cancel_order GET /orders/:id/cancel(.:format) {:action=>"cancel", :controller=>"orders"}
orders GET /orders(.:format) {:action=>"index", :controller=>"orders"}
POST /orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET /orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET /orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET /orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
[...]
EDIT#2:
Maybe when I call confirm_order_url Rails doesn’t know how to generate the route which should be like “orders/:id/confirm” because @order.id hasn’t been assigned yet.
But I am calling this method after @order.save.
Namely, here:
if @order.save
response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(@order.price_in_cents,
:ip => @order.ip_address,
:return_url => confirm_order_url,
:cancel_return_url => cancel_order_url
)
redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)
So this leads to a subquestion: is @order.id set after the call to @order.save?
And if not, how can I set it properly before calling an helper method such ad confirmation_order_url?
SOLUTION
Thanks to Jayendra Patil i fixed my code this way:
if @order.save
response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(@order.price_in_cents,
:ip => @order.ip_address,
:return_url => confirm_order_url(@order),
:cancel_return_url => cancel_order_url(@order)
)
redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)
I was wrongly assuming that Rails could guess for which @order I was calling a “member” url, so the answer is to pass @order as the argument for member routes. Thank you.
This generates routes as –
you should use the order object or id with these routes –
Also Rails automatically assigns the id to the instance after it is saved so @order.id should work.