1

我有一個部分,我想刪除或不顯示數組中的前三篇文章,因爲它們在精選文章部分。我也希望部分使用will_paginate w /無限滾動來加載下一頁文章。我面臨的問題是,當使用@articles.drop(3).each do |a|並下一頁加載時,該陣列會再次丟棄接下來的三篇文章。Rails will_paginate無限滾動與丟棄前3項的數組

解決這個問題的最佳方法是什麼?我最初的想法是一個數組內的數組,第一個數組放下第一個3,然後嵌套數組返回所有文章,但我不知道該怎麼做?在部分

陣列碼:

<% @articles.drop(3).each do |a| %> 
    <%= link_to a.source_url, :class => "flexRow" do %> 
    <%= a.source %> 
    <h3><%= a.title %></h3> 
    <% end %> 
<% end %> 

index.js.erb的

$('#article-index').append(' <%= j render("articles") %>'); 

<% if @articles .next_page %> 
    $('.pagination').replaceWith('<%= j will_paginate(@articles, :previous_label => '', :next_label => '', :page_links => false) %>'); 
<% else %> 
    $('.pagination').remove(); 
<% end %> 

Index.html.erb

<div id="article-index"> 
<%= render 'articles' %> 
</div> 

UPDATE 該解決方案似乎工作,但沒有按」你覺得優雅嗎?

<% (@articles.current_page == 1 ? @articles.drop(3) : @articles).each do |a| %> 

回答

1

嘗試

@articles[[email protected]] 

這將下降,在指數0,1和2日舉行的記錄,並歸還剩餘。

+0

感謝但這會導致一些funkiness與分頁,不是所有的文章出現。 –

0

你可以做你的控制器以下幾點:

@articles = Article.where(...).paginate(page: params[:page], per_page: 10) 

# Works only for the first HTML request 
unless request.xhr? 
    @articles.shift(3) 
end 
. 
. 
. 
respond_to do |format| 
    format.html 
    format.js 
end 

現在,當你遍歷 @articles,它會從指數3日開始,只對第一次。

編輯

<% (request.xhr? ? @articles : @articles[3..10]).each do |a| %> 
    <%= link_to a.source_url, :class => "flexRow" do %> 
    <%= a.source %> 
    <h3><%= a.title %></h3> 
    <% end %> 
<% end %> 

假設頁面大小爲10

+0

嗯,當在控制器中嘗試時,我得到#

的'未定義方法'shift'。 –

+0

@ArashHadipanah你是對的!這是因爲它不是一個數組。 – Abhi

+0

試試編輯答案 – Abhi