0

当我第一次使用 jquery 数据表访问该页面时,它工作正常,但是在访问该页面后,我去或重新加载任何页面后,我的堆栈级别都太深了。我关注了 http://railscasts.com/episodes/340-datatables

这是我的代码

include Rails.application.routes.url_helpers
class BookingsDatatable

  delegate :params, :link_to, to: :@view

  def initialize(view)
    @view = view
  end

  def as_json(options = {})
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: Booking.count,
      iTotalDisplayRecords: bookings.total_entries,
      aaData: data
    }
  end

private

  def data
    bookings.map do |booking|
      [
        booking.booked_by,
        booking.client_ref,
        booking.booking_type,
        booking.start_date,
        booking.end_date,
       link_to("Invoices", booking_invoices_path(booking)),
       link_to( "Edit", edit_booking_path(booking))
      ]
    end
  end

  def bookings
    @bookings ||= fetch_bookings
  end

  def fetch_bookings
    bookings = Booking.order("#{sort_column} #{sort_direction}")
    bookings = bookings.page(page).per_page(per_page)
    if params[:sSearch].present?
      bookings = bookings.where("client_ref like :search or booked_by like :search", search: "%#{params[:sSearch]}%")
    end
    bookings
  end

  def page
    params[:iDisplayStart].to_i/per_page + 1
  end

  def per_page
    params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
  end

  def sort_column
    columns = %w[booked_by client_ref start_date end_date booking_type]
    columns[params[:iSortCol_0].to_i]
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end
end

当我在类中包含模块 Rails.application.routes.url_helpers 时它工作正常,但如果它在类外部,我会得到这个错误堆栈级别太深(在第一页访问时工作正常,但之后出错)。我不知道为什么会这样。

4

1 回答 1

0

堆栈级别太深意味着您在 99% 的情况下都存在循环依赖问题。我猜当您包含 url 帮助程序时,它会自动加载所有能够生成帮助程序的类,如果其中一个类中有包含,它会进入无限递归。该问题已在前面记录,但在没有补丁的情况下存档:

https://github.com/rails/rails/issues/3144

于 2013-08-09T07:50:23.477 回答