Rails 5:获取订单的完整计数



我想在预订视图中显示所有订单和用户的总收入。

我试过预订。count(:all(,但这似乎不起作用。有没有办法统计所有用户的订单

这是我目前的预订管理员,没有任何更改:

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>

在控制器中,您应该调用:

@count = Reservation.count()
@total = Reservation.sum(:total)

然后在视图中:

Reservations Count: <%= @count %>
Reservations Total: $ <%= @total %>

Ruby On Rails API文档用于计数并求和

明确表示您正在计算所有预订的替代语法是:

@count = Reservation.all.count

就我个人而言,我喜欢尽可能清楚地表明我对查询的期望。比如,如果将来你只想计算未取消的预订,或者可能只计算特定月份的预订,该怎么办?

R.Sierra的其余回答很中肯。

最新更新