Rails:将与current_user分页

时间:2018-11-02 13:03:44

标签: ruby-on-rails ruby-on-rails-5 will-paginate

我目前正在尝试将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) %>

更新 现在它可以在wine控制器中运行,但是当我想将其实现到Reservations控制器,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

0 个答案:

没有答案