Rails 5: старое значение не обновляется в базе данных - PullRequest
0 голосов
/ 27 октября 2018

Я сейчас пытаюсь сохранить обновленное значение в базе данных.

Процесс следующий: пользователь выбирает купить 3 бутылки, запас уменьшается на -3. Запас устанавливается в контроллере Wines и затем отображается в контроллере резервирования для пользователя

В настоящее время проблема заключается в том, что для каждого нового резервирования сбрасывается запас товара в контроллере резервирования, поскольку запас не обновляется в контроллере вина.

Здесь кроется проблема:

@reservation.in_stock = wine.in_stock - @reservation.bottle

Вот мой полный контроллер бронирования:

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.in_stock = wine.in_stock - @reservation.bottle
    @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 set_stock
    @reservation.in_stock = wine.in_stock
  end

  def reservation_params
    params.require(:reservation).permit(:start_date, :bottle, :in_stock)
  end
end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...