Rails: будет разбит на страницы с current_user - PullRequest
0 голосов
/ 02 ноября 2018

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

Я ищу что-то, чтобы совместить current_user.wines с Wine.paginate ..

В настоящее время я понятия не имею, как реализовать это

Мой контроллер вина:

  def index
    @wines = current_user.wines
    @wines = Wine.paginate(page: params[:page])
  end

Как вы видите, я сейчас переопределяю @wines ...

Моя модель вина:

class Wine < ApplicationRecord
  enum instant: {Reservieren: 0, Sofort: 1}
  belongs_to :user
  self.per_page = 1
end

Мой индекс просмотра:

<div class="container-small">
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        Deine Weine
      </div>
      <div class="panel-body">
        <% if !current_user.admin? %>
        <% current_user.favorites.each do |favorite| %>
        <div class="row">
          <div class="col-md-2">
            <%= image_tag favorite.wine.cover_photo(:thumb) %>
          </div>
          <div class="col-md-7">
            <h4><%= favorite.wine.wine_name %></h4>
          </div>
          <div class="col-md-3 right">
            <%= link_to "Bestellen", wine_path(@wines), class: "btn btn-form" %>
          </div>
        </div>
        <hr/>
        <% end %>
        <% else %>
        <% @wines.each do |wine| %>
        <div class="row">
          <div class="col-md-2">
            <%= image_tag wine.cover_photo(:thumb) %>
          </div>
          <div class="col-md-7">
            <h4><%= wine.wine_name %></h4>
          </div>
          <div class="col-md-3 right">
            <%= link_to "Bearbeiten", details_wine_path(wine), class: "btn btn-form" %>
          </div>
        </div>
        <hr/>
        <% end %>
        <% end %>
      </div>
    </div>
  </div>
</div>
</div>
<%= will_paginate(@wines) %>

UPDATE Теперь он работает в контроллере Wine, но когда я хочу внедрить его в контроллер резервирования, в методы your_orders и your_reservations, он не отображается в представлении. Я не получаю сообщение об ошибке, когда звоню <%= will_paginate(@wines) %>

Вид бронирования:

<div class="row" id="orders">
  <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">
              <% if reservation.Bearbeitung? %>
              <div class="col-md-3">
                Reserviert am<br/>
                <%= reservation.start_date.strftime('%v') %>
              </div>
              <% else %>
              <div class="col-md-3">
                Erwartet am<br/>
                <%= reservation.start_date.strftime('%v') %>
              </div>
              <% end %>
              <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 %>€
              </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><!-- row -->
            <hr/>
          <% end %>
        <% end %>
      </div><!-- panel body -->
    </div><!-- panel default -->
  </div><!-- col md 12 -->
</div><!-- row -->
</div><!-- row -->
</div><!-- container -->
<%= will_paginate(@wines) %>

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

  def your_orders
    @orders = current_user.reservations.order(start_date: :asc)
  end

  def your_reservations
      @wines = current_user.wines.paginate(page: params[:page])
      redirect_to root_path unless current_user.admin == true
      @count = Reservation.count(:all)
      @total = Reservation.sum(:total)
  end
...