2015-12-21 85 views
3

我正在使用ransack進行搜索,現在我想在我的rails應用程序中實現分頁。所以我使用了will_paginate gem。我面臨的問題是,我無法弄清楚如何將paginate放入當前控制器代碼中,因爲它已根據查詢獲取結果。如何在rails中使用will_paginate和ransack

這裏是我的控制器代碼

def search 
if params[:search].present? && params[:search].strip != "" 
    session[:loc_search] = params[:search] 
end 
arrResult = Array.new 
if session[:loc_search] && session[:loc_search] != "" 
    @rooms_address = Room.where(active: true).near(session[:loc_search], 5, order: 'distance') 
else 
    @rooms_address = Room.where(active: true).all 
end 

@search = @rooms_address.ransack(params[:q]) 
@rooms = @search.result 

@arrRooms = @rooms.to_a 

有人能告訴我如何把分頁這裏?

原木

18:29:40 web.1 | Room Load (0.8ms) SELECT rooms.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((12.9715987 - rooms.latitude) * PI()/180/2), 2) + COS(12.9715987 * PI()/180) * COS(rooms.latitude * PI()/180) * POWER(SIN((77.5945627 - rooms.longitude) * PI()/180/2), 2))) AS distance, MOD(CAST((ATAN2(((rooms.longitude - 77.5945627)/57.2957795), ((rooms.latitude - 12.9715987)/57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "rooms" WHERE "rooms"."active" = $1 AND (rooms.latitude BETWEEN 12.754501025333727 AND 13.188696374666272 AND rooms.longitude BETWEEN 77.37177993269385 AND 77.81734546730614 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((12.9715987 - rooms.latitude) * PI()/180/2), 2) + COS(12.9715987 * PI()/180) * COS(rooms.latitude * PI()/180) * POWER(SIN((77.5945627 - rooms.longitude) * PI()/180/2), 2)))) BETWEEN 0.0 AND 15) ORDER BY distance LIMIT 5 OFFSET 5 [["active", "t"]] 
18:29:40 web.1 | (0.3ms) SELECT COUNT(*) FROM "rooms" WHERE "rooms"."active" = $1 AND (rooms.latitude BETWEEN 12.754501025333727 AND 13.188696374666272 AND rooms.longitude BETWEEN 77.37177993269385 AND 77.81734546730614 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((12.9715987 - rooms.latitude) * PI()/180/2), 2) + COS(12.9715987 * PI()/180) * COS(rooms.latitude * PI()/180) * POWER(SIN((77.5945627 - rooms.longitude) * PI()/180/2), 2)))) BETWEEN 0.0 AND 15) [["active", "t"]] 

回答

7

你分頁搜索的結果,所以ransack後更新。 像@rooms = @search.result.paginate(page: params[:page], per_page: params[:per_page])應該工作。

+0

感謝它的工作..但不是最好先分頁或...在這種情況下這是最好的辦法? – Abhilash

+0

不是真的 - ransack也可以做排序,而不僅僅是搜索,而且你通常不希望只排序結果的當前頁面。 – eugen

+0

因此在使用ransack的時候使用willpaginate是一件矯枉過正的事情? – Abhilash

相關問題