Kris Kris - 4 months ago 10
Ruby Question

Rails: how to show only selected order on show page?

I am having trouble trying to get my orders show page to show only the order that is selected with all of the order items that belong to to that order. For example: /orders/8 should only show order #8. My current code shows all of the orders when I go to this link. I am pretty sure I need to replace the .all in the show section of my controller with something else but I cant seem to figure out what that might be?

The associations are:
orders have many order_items
order_items belong to an order


Here is the current show page view:

<% @orders.each do |order| %>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><p>Order Total = <%= order.total %><p/></h3>
</div>
<div class="panel-body">

<% order.order_items.each do |order_item| %>

<p>Order ID = <%= order_item.order_id %>
Product ID = <%= order_item.product_id %>
Unit Price = <%= order_item.unit_price %></p>

<% end %>
</div>
</div>
<% end %>


Orders controller:

class OrdersController < ApplicationController
def index
@orders = Order.all

@order_items = OrderItem.all



end

# GET /posts/1
# GET /posts/1.json
def show

@orders = Order.all

end

end


Thanks in advance for any help!

Answer
def show
 @order = Order.find(params[:id])
end

also change @orders to @order in show page.