Rails 5: jquery datepicker отображает входное значение при выбранной дате - PullRequest
0 голосов
/ 22 октября 2018

Я пытаюсь отобразить входное значение «бутылок» в разделе предварительного просмотра, когда пользователь выбирает дату.

Поле ввода «бутылки» отображается, но когда пользователь выбирает, например, 3 бутылки, они не отображаются в разделе предварительного просмотра.

My _form.html.erb

  <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 :bottles, 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="reservations_bottles"></span></td>
            </tr>
            <tr>
              <td class="total">Total</td>
              <td class="text-right"><span id="bottles_total"></span> €</td>
            </tr>
          </tbody>
        </table>
      </div>

      <br/>
      <%= f.submit "Bestellen", id: "btn_book", class: "btn btn-normal btn-block", disabled: true %>
    <% end %>
  </div>
</div>

<script>

$(function() {

  $.ajax({
    url: '<%= preload_wine_path(@wine) %>',
    dateTyp: 'json',
    success: function(data) {

      $.each(data, function(arrID, arrValue) {

      });

      $('#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);

          var bottles = document.getElementById("bottles").value;
          var total = bottles * <%= @wine.price %>

          $('#reservation_bottles').text(bottles);
          $('#bottles_total').text(total);
        }
      });
    }
  });
});

</script>

Контроллер резервирования:

class ReservationsController < ApplicationController
  before_action :authenticate_user!

  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.reservation.build(reservation_params)
    @reservation.wine = wine
    @reservation.price = wine.price
    @reservation.total = wine.price * wine.bottles
    @reservation.save

    flash[:notice] = "Erfolgreich Bestellt!"
  end
    redirect_to wine
  end

  private
  def reservation_params
    params.require(:reservations).permit(:start_date)
  end

end

Помощник по применению:

module ApplicationHelper
  def avatar_url(user)
    gravatar_id = Digest::MD5::hexdigest(user.email).downcase
    "https://www.gravatar.com/avatar/#{gravatar_id}.jpg?d=identical&s=150"
  end
  def wine_quantity(wine)
    case
    when wine.is_1 && wine.is_3 && wine.is_6 && wine.is_12 then [["1 Flasche", 1], ["3 Flaschen", 3], ["6 Flaschen", 6], ["12 Flaschen", 12]]
    when wine.is_1 && wine.is_3 && wine.is_6 then [["1 Flasche", 1], ["3 Flaschen", 3], ["6 Flaschen", 6]]
    when wine.is_1 && wine.is_3 && wine.is_12 then [["1 Flasche", 1], ["3 Flaschen", 3], ["12 Flaschen", 12]]
    when wine.is_1 && wine.is_6 && wine.is_12 then [["1 Flasche", 1], ["6 Flaschen", 6], ["12 Flaschen", 12]]
    when wine.is_1 && wine.is_12 then [["1 Flasche", 1], ["12 Flaschen", 12]]
    when wine.is_1 && wine.is_6 then [["1 Flasche", 1], ["6 Flaschen", 6]]
    when wine.is_3 && wine.is_3 then [["1 Flasche", 1], ["3 Flaschen", 3]]
    else
      end
    end
  end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...