当前,我在覆盖数据库中的值时遇到问题。
提供商可以设置每种葡萄酒的库存量,例如23种,现在前端用户可以选择不同的葡萄酒,例如3瓶。
现在,我要将库存值从23更新为20。如果库存为0,我要禁用定购按钮。
我正在考虑使用read_attribute(attr_name)和write_attribute(attr_name,value)。这是正确的方法吗?
这是我的预订控制人:
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
@count = Reservation.count(:all)
@total = Reservation.sum(:total)
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, :in_stock)
end
end
我的预订视图:
<div class="panel panel-default">
<div class="panel-heading" style="text-align:center">
<span class="price-period" style="border:none; font-size:12px">1 Flasche<br/><%= @wine.price %> €</span>
<span class="price-period" style="font-size:14px">3 Flaschen<br/><%= @wine.price * 3 %> €</span>
<span class="price-period" style="font-size:12px">6 Flaschen<br/><%= @wine.price * 6 %> €</span>
</div>
<div class="panel-body">
<%= form_for([@wine, @wine.reservations.new]) do |f| %>
<div class="panel-body">
<div class="col-md-12">
<label>Lieferdatum</label>
<%= f.text_field :start_date, readonly: true, placeholder: "Lieferdatum", class: "form-control datepicker" %>
</div>
<!-- BUY SET? -->
<div class="col-md-12 select" style="margin-top:10px">
<div class="form-group">
<label>Anzahl an Flaschen</label>
<%= f.select :bottle, wine_quantity(@wine), {}, id: "bottles", prompt: "Auswählen...", class: "form-control" %>
</div>
</div>
</div>
<div id="preview" style="display:none">
<table class="reservation-table">
<tbody>
<tr>
<td>Preis pro Flasche</td>
<td class="text-right"><%= @wine.price %>€</td>
</tr>
<tr>
<td>Anzahl Flaschen</td>
<td class="text-right">x <span id="reservation_bottles"></span></td>
</tr>
<tr>
<td class="total">Gesamt</td>
<td class="text-right"><span id="reservation_total"></span>€</td>
</tr>
</tbody>
</table>
</div>
<br/>
<!-- Disable Button if in_stock = 0 -->
<% if @wine.Sofort? %>
<%= f.submit "Bestellen", id: "btn_book", class: "btn btn-normal btn-block", disabled: true %>
<% else %>
<%= f.submit "Reservieren", id: "btn_book", class: "btn btn-normal btn-block", disabled: true %>
<% end %>
<% end %>
</div>
</div>
<script>
$(function() {
$.ajax({
url: '<%= preload_wine_path(@wine) %>',
dataTyp: 'json',
success: function(data) {
var bottles = document.getElementById("bottles").value;
var total = bottles * <%= @wine.price %>
$('#reservation_bottles').text(bottles);
$('#reservation_total').text(total.toFixed(2));
$('#reservation_start_date').datepicker({
dateFormat: 'dd-mm-yy',
minDate: 2,
maxDate: '5d',
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(selected) {
$('#preview').show();
$('#btn_book').attr('disabled', false);
$('#bottles').on('click', function() {
var bottles = $(this).val();
var total = bottles * <%= @wine.price %>
$('#reservation_bottles').text(bottles);
$('#reservation_total').text(total.toFixed(2));
});
}
});
}
});
});
</script>