gpt4 book ai didi

ruby-on-rails-4 - 如何在 rails 中使用 will_paginate 和 ransack

转载 作者:行者123 更新时间:2023-12-04 23:18:19 25 4
gpt4 key购买 nike

我正在使用 ransack 进行搜索,现在我想在我的 rails 应用程序中实现分页。所以我正在使用 will_paginate gem。我面临的问题是我无法弄清楚如何将分页放在我当前的 Controller 代码中,因为它已经根据查询获取了结果。

这是我的 Controller 代码

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"]]

最佳答案

您对搜索结果进行分页,因此在 ransack 之后.
类似 @rooms = @search.result.paginate(page: params[:page], per_page: params[:per_page])应该管用。

关于ruby-on-rails-4 - 如何在 rails 中使用 will_paginate 和 ransack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34395419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com