我想在预订视图中显示所有订单以及为用户赚取的总金额。
我尝试了Reservation.count(:all),但这似乎不起作用。有没有办法计算来自所有用户的订单?
这是我当前的Reservations控制器,没有任何更改:
class ReservationsController < ApplicationController
before_action :authenticate_user!
before_action :set_reservation, only: [:approve, :decline]
def create
wine = Wine.find(params[:wine_id])
if current_user == wine.user
flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
else
start_date = Date.parse(reservation_params[:start_date])
@reservation = current_user.reservations.build(reservation_params)
@reservation.wine = wine
@reservation.price = wine.price
@reservation.total = wine.price * @reservation.bottle
# @reservation.save
if @reservation.save
if wine.Reservieren?
flash[:notice] = "Anfrage versendet!"
else
@reservation.Bearbeitung!
flash[:notice] = "Eroflgreich bestellt!"
end
else
flash[:alert] = "Can't make a reservation!"
end
end
redirect_to wine
end
def your_orders
@orders = current_user.reservations.order(start_date: :asc)
end
def your_reservations
@wines = current_user.wines
redirect_to root_path unless current_user.admin == true
end
def your_upcoming_reservations
@wines = current_user.wines
redirect_to root_path unless current_user.admin == true
end
def approve
@reservation.Versendet!
redirect_to your_reservations_path
redirect_to root_path unless current_user.admin == true
end
def decline
@reservation.Abgelehnt!
redirect_to your_reservations_path
redirect_to root_path unless current_user.admin == true
end
# Each Order Details
def order_details
@orders = current_user.reservations.order(start_date: :asc)
end
private
def set_reservation
@reservation = Reservation.find(params[:id])
end
def reservation_params
params.require(:reservation).permit(:start_date, :bottle)
end
end
这是我当前的观点:
<div class="container-small">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Alle Bestellungen
</div>
<div class="panel-body">
<% @wines.each do |wine| %>
<% wine.reservations.each do |reservation| %>
<div class="row">
<div class="col-md-3">
Erwartet am<br/>
<%= reservation.start_date.strftime('%v') %>
</div>
<div class="col-md-2">
<p><%= reservation.status %></p>
<div class="form-inline">
<% if reservation.Bearbeitung? %>
<%= link_to approve_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-up fa-lg"></i> <% end %> |
<%= link_to decline_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-down fa-lg"></i> <% end %>
<% end %>
</div>
</div>
<div class="col-md-4">
<%= reservation.bottle %>x
<%= link_to reservation.wine.wine_name, wine_path(reservation.wine) %><br/>
Gesamt: <%= reservation.total %>€<br/>
</div>
<div class="col-md-3 pull-right">
Lieferung an<br/><br/>
<span>
<%= link_to user_path(reservation.user) do %>
<%= reservation.user.fullname %><br/>
<% end %>
<%= reservation.user.location %>
</span>
</div>
</div>
<hr/>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:2)
在控制器中,您应该致电:
@count = Reservation.count()
@total = Reservation.sum(:total)
然后在视图中:
Reservations Count: <%= @count %>
Reservations Total: $ <%= @total %>
答案 1 :(得分:2)
明确表示您正在计算所有预留的另一种语法是:
@count = Reservation.all.count
就我个人而言,我希望对查询的期望尽可能地明显。例如,如果将来您只想计算未取消的预订,或者也许只计算特定月份的预订,该怎么办?
R。Sierra的其余答案很明确。