Skip to content

Commit 309895d

Browse files
committed
Added Orders => Edit + Destroy
1 parent 3e83309 commit 309895d

6 files changed

Lines changed: 114 additions & 11 deletions

File tree

app/controllers/orders_controller.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class OrdersController < ApplicationController
22
before_action :logged_in_user, only: %i[index show new create]
3+
before_action :user_is_admin, only: %i[destroy edit]
34

45
def index
56
@orders = Order.all
@@ -33,7 +34,39 @@ def create
3334
session[:cart_id] = nil
3435
redirect_to orders_path
3536
# byebug
36-
end
37+
end
38+
39+
# def destroy
40+
# respond_to do |format|
41+
# if @order.destroy!
42+
# format.html { redirect_to products_url }
43+
# format.json { head :no_content }
44+
# flash[:info] = 'Order was successfully destroyed.'
45+
# else
46+
# flash[:info] = 'Error destroying the order'
47+
# end
48+
# end
49+
# end
50+
51+
def destroy
52+
@order = Order.find(params[:id])
53+
flash[:info] = 'Found product to destroy'
54+
# @order.destroy
55+
# respond_to do |format|
56+
# format.html { redirect_to orders_path, notice: 'Order was successfully destroyed.' }
57+
# format.json { head :no_content }
58+
# flash[:info] = 'Order was successfully destroyed.'
59+
# end
60+
end
61+
62+
def edit
63+
@order = Order.find(params[:id])
64+
end
65+
66+
def update
67+
@order = Order.update(order_params)
68+
redirect_to orders_path
69+
end
3770

3871
private
3972

app/controllers/products_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def create
2929

3030
respond_to do |format|
3131
if @product.save
32-
format.html { redirect_to @product, notice: 'Product was successfully created.' }
32+
format.html { redirect_to @product }
3333
format.json { render :show, status: :created, location: @product }
3434
flash[:info] = 'Product was successfully created.'
3535
else
@@ -44,7 +44,7 @@ def create
4444
def update
4545
respond_to do |format|
4646
if @product.update(product_params)
47-
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
47+
format.html { redirect_to @product}
4848
format.json { render :show, status: :ok, location: @product }
4949
flash[:info] = 'Product was successfully updated.'
5050
else
@@ -59,7 +59,7 @@ def update
5959
def destroy
6060
@product.destroy
6161
respond_to do |format|
62-
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
62+
format.html { redirect_to products_url }
6363
format.json { head :no_content }
6464
flash[:info] = 'Product was successfully destroyed.'
6565
end

app/views/orders/_order.html.erb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h3>
2-
<%= link_to ( gravatar_for User.find_by(id:order.user_id), size: 100 ) , User.find_by(id:order.user_id) %>
2+
<%= link_to ( gravatar_for User.find_by(id:order.user_id), size: 100 ) , User.find_by(id:order.user_id) %>
33
Order ID
44
<%= order.id %>
55
, User ID
@@ -16,12 +16,13 @@
1616
<strong>Pay_method:</strong>
1717
<%= order.pay_method %></p>
1818
<p>
19-
<strong> Description</strong>
19+
<strong>
20+
Description</strong>
2021
<div class="well well-sm"><%= order.description %></div>
2122
</p>
2223
<p>
2324
<h3>Total price:
24-
<%= number_to_currency(order.sub_total) %></h3>
25+
<%= number_to_currency(order.sub_total) %></h3>
2526
</p>
2627

2728
<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#<%= order.id %>">Show Items</button>
@@ -35,4 +36,19 @@
3536
<br/>
3637
</ul>
3738
</div>
39+
40+
<!-- Only Admin is allowed to modify Orders -->
41+
<% if logged_in? && current_user.admin? %>
42+
<%= link_to edit_order_path(order) do %>
43+
<i class="glyphicon glyphicon-pencil" style="color:darkgray"></i>
44+
Edit
45+
<% end %>
46+
or
47+
<!-- Basket -->
48+
<%= link_to order, method: :delete, data: { confirm: 'Are you sure?' } do %>
49+
<i class="glyphicon glyphicon-fire" style="color:firebrick"></i>
50+
Destroy
51+
<% end %>
52+
<% end %>
53+
3854
<hr/>

app/views/orders/_order2.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h3>
2+
<%= link_to ( gravatar_for User.find_by(id:order.user_id), size: 100 ) , User.find_by(id:order.user_id) %>
3+
Order ID
4+
<%= order.id %>
5+
, User ID
6+
<%= order.user_id %></h3>
7+
<p>
8+
<strong>Name:</strong>
9+
<%= User.find_by(id:order.user_id).name %>
10+
11+
,
12+
<strong>Email:</strong>
13+
<%= User.find_by(id:order.user_id).email %>

app/views/orders/edit.html.erb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<%= render 'order2',:order => @order %>
2+
3+
<div class="center jumbotron">
4+
<%= form_for(@order) do |f| %>
5+
<p style="margin-top:25px">
6+
<%= f.label :pay_method %><br>
7+
<%= f.text_field :pay_method, required: true, autofocus: true %>
8+
</p>
9+
<p>
10+
<%= f.label :description %><br>
11+
<%= f.text_field :description %>
12+
</p>
13+
<p>
14+
<%= f.submit "Update Order"%>
15+
</p>
16+
17+
<% end %>
18+
19+
<p>
20+
<h3>Total price:
21+
<%= number_to_currency(@order.sub_total) %></h3>
22+
</p>
23+
24+
<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#<%= @order.id %>">Show Items</button>
25+
<div id="<%= @order.id %>" class="collapse">
26+
<ul class="users">
27+
<% @order.line_items.each do |item| %>
28+
<%= item.quantity %>
29+
x
30+
<%= render 'shared/order_product' , product:item.product%>
31+
<% end %>
32+
<br/>
33+
</ul>
34+
</div>
35+
</div>

app/views/products/_product.html.erb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ $
3636

3737
<!-- Only Admin is allowed to modify products -->
3838
<% if logged_in? && current_user.admin? %>
39-
<%= link_to 'Edit', edit_product_path(product) %>
40-
or
41-
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
39+
<%= link_to edit_product_path(product) do %>
40+
<i class="glyphicon glyphicon-pencil" style="color:darkgray"></i>
41+
Edit
42+
<% end %>
43+
or
44+
<!-- Basket -->
45+
<%= link_to product, method: :delete, data: { confirm: 'Are you sure?' } do %>
46+
<i class="glyphicon glyphicon-fire" style="color:firebrick"></i>
47+
Destroy
48+
<% end %>
4249
<% end %>
43-
4450
</p>
4551
</li>

0 commit comments

Comments
 (0)