[英]Ruby on rails, ajax, offset, limit. undefined method `each' for nil:NilClass
我正在嘗試做類似鏈接“更多”的事情。 但是我在視圖“ nil:NilClass的未定義方法'each'”中遇到錯誤
控制者
WHO_IS_OUT_LIST_PER_PAGE = 6
def show_who_is_out_list
@offset = params[:offset].to_i
get_who_is_out_list
render 'show_who_is_out_list', layout: false
end
private
def get_who_is_out_list
data = current_company.time_off_requests.page(@offset, WHO_IS_OUT_LIST_PER_PAGE)
@offset = (data.length < WHO_IS_OUT_LIST_PER_PAGE) ? nil : @offset + WHO_IS_OUT_LIST_PER_PAGE
@who_is_out_list = data
end
在模型中我有一個范圍
scope :page, ->(offset, limit) { order("start_date desc").limit(limit).offset(offset) }
在視圖show_who_is_out_list-js中
:plain
$("#who-is-out").html("#{escape_javascript(render 'who_is_out')}");
這是有錯誤的部分
%header.box-header-title.clearfix
%h1.header-h1 Who’s out?
%a.box-header-link{:href => "/time_off_requests/manage"} View Calendar
%ul.list.sidebar-list
%li.clearfix
- @who_is_out_list.each do |t|
%td= t.user.full_name
-if (t.start_date == t.end_date and t.start_date >= Date.today)
%span= t.start_date.strftime("%b %d")
%br
-else
%span= "#{t.start_date.strftime("%b %d")}"' - '"#{t.end_date.strftime("%b %d")}"
%br
%li.clearfix
= link_to t('Back'), show_who_is_out_list_path(offset: 0), remote: true, class: 'btn'
- if @offset.present?
= link_to t('next'), show_who_is_out_list_path(offset: @offset), remote: true, class: 'btn'
路線
resource :time_off_request, only: :all do
get 'show_who_is_out_list'
end
怎么了? 我幾乎是初級RoR開發人員,我必須用ajax編寫它(您能幫我嗎?
我假設這里的問題是@who_is_out_list = data
因為您只在上面調用each
。
嘗試這個:
@who_is_out_list = data if data.exists?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.