Order summary page

Instead of redirecting to root_url after buying an item, I want to create a page that shows a confirmation basically saying "Thanks for your order. Your total was: $x. " How would I go about doing that? Thanks

Posted almost 5 years ago by Markus Lopez
Posted almost 5 years ago by Alex Yang

You'll need to create a new page in your app. Here are the steps:

1) Create a new action from within your Orders controller file (e.g. def confirmation...). Also don't forget to add ':confirmation' to the 'set_order' in the 'before_action' at the top.
2) Create a new view page with the same name (e.g. views/orders/confirmation.html.erb)
3) Add this new 'confirmation' page to your config/routes.rb file (You should now be able to see the path by running 'rake routes', and you should also be able to visit this blank page in your browser)
4) Replace 'root_url' with your new confirmation route

Try it out and let me know how it goes!

0
Posted almost 5 years ago by Markus Lopez

1) What should I make my def confirmation as?
2) I'm getting this error:
ActiveRecord::RecordNotFound in OrdersController#confirmation, Couldn't find Order without an ID, this line highlighted: def set_order
@order = Order.find(params[:id])
end

Thanks

0
Posted almost 5 years ago by Alex Yang

1) You can model your 'def confirmation' off the other controller-actions (e.g. 'index').
2) You're seeing the error because your new confirmation route should take an ':id' as an input. Make sure that you passed the id in the path (see 'rake routes' for the right format). If you've done that and it still doesn't work, you should also make sure that you set up the route correctly (in your config/routes.rb) so that it accepts this format.

0
Posted almost 5 years ago by Markus Lopez

I've been fiddling with it all day and feel like I've tried everything.

I'd appreciate if you could take a look.

here's the latest thing I tried:
route:
get '/listing/:id/orders/confirmation/', to: "orders#confirmation"

here's the controller:

def confirmation
@order = Order.find(params[:order_id])
@listing = Listing.find(params[:listing_id])
end

and the redirect path:

respond_to do |format|
if @order.save
format.html { redirect_to confirmation_path}
...

This is just one example of what I've tried. Thanks for any advice

0
Posted almost 5 years ago by Alex Yang

Haven't tested the below, but try it and let me know how it goes. In your routes.rb:

resources :listings do
resources :orders, only: [:new, :create] do
member do
get 'confirmation'
end
end
end

This 'member' section allows you to create new actions that apply to a single order (use 'collection' if you need an action that applies to multiple orders).

In your controller, create a section like this:

def confirmation
end

At the top, make sure that the ':confirmation' action has been added to the 'set_order' line:

before_action :set_order, only: [:confirmation, :show, :edit, :update, :destroy]

Then, instead of using 'root_url', replace it with something like the following:

confirmation_listing_order_url(@order.listing.id, @order.id)

0