分页嵌套资源

时间:2014-08-06 19:39:20

标签: ruby-on-rails ruby

我使用Ruby on Rails 4.1创建了一个票务预订应用程序作为我的示例项目。三个是三个模型 - 活动,门票和预订。活动有很多门票和预订。门票有很多预订,属于活动。预订属于活动和门票。

路线文件如下:

Rails.application.routes.draw do

  devise_for :users
  resources :charges

  root 'events#index'

  resources :events do
    resources :tickets
    resources :bookings
  end
end

现在,我使用了will_paginate gem来组织Events和Bookings的索引页面。在事件的情况下,它非常简单,分页工作正常。但是,当涉及到嵌套资源的预订时,索引页面具有分页链接,但所有页面都显示所有预订条目。根本没有分页。

预订控制器看起来像:

class BookingsController < ApplicationController
  before_action :authenticate_user!, only: [:index, :destroy]

  def index
    @event = Event.find(params[:event_id])
    @bookings =@event.bookings.paginate(page: params[:page], per_page: 5)
  end

和预订指数:

<h2>All Bookings</h2>

  <div class="row">
    <div class="col-md-10">
      <table class="table">
        <thead>
              <tr>
                  <th>Buyer Name</th>
                  <th>Email Address</th>
                  <th>Ticket Type</th>
                  <th>No. of Tickets</th>
                  <th>Amount</th>
                  <th></th>
              </tr>
          </thead>
          <tbody>
            <% @event.bookings.each do |booking| %> 
              <tr> 
                  <td><%= booking.buyer_name %></td>
                  <td><%= booking.email %></td>
                  <td><% unless booking.ticket.blank? %>
                        <%= booking.ticket.ticket_name rescue nil %>
                      <% else %>
                         No Ticket
                      <% end %>
                  </td>
                  <td><%= booking.order_quantity %></td>
                  <td><% unless booking.ticket.blank? %>
                        <%= number_to_currency(booking.total_amount) %>
                    <% else %>
                      No Amount
                    <% end %>
                  </td>                  
                  <td><%= link_to "Delete", event_booking_path(@event, booking), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-link" %></td>
            <% end %>
          </tr>
      </table>
    </div>
  </div>
<%= will_paginate @bookings %>
<%= link_to "Back to Event", event_path(@event), class: "btn btn-link" %>
<%= link_to "All Bookings", event_bookings_path(@event), class: "btn btn-link" %>

当我将鼠标悬停在分页链接上时,会保留正确的网址结构。我使用的桌子是否有机会挡住?

1 个答案:

答案 0 :(得分:1)

分页的事件列表存储在@bookings变量中,但是在您的表中,您正在迭代@event.bookings集合,该集合将被取消激活。

更改此

<% @event.bookings.each do |booking| %> 

到这个

<% @bookings.each do |booking| %> 
相关问题