[英]Rails 5: Exporting Ransack result set with axlsx
I'm trying generate Excel file using axlsx gem based on Ransack gem result set.我正在尝试使用基于 Ransack gem 结果集的 axlsx gem 生成 Excel 文件。
controller:控制器:
@q = Candy.ransack(params[:q])
@candies = @q.result.all
When I call @candies with parameters like "chocolate" in the view using Ransack gem, I get 30 or so results out of 600. It was successfully filtered!当我使用 Ransack gem 在视图中使用诸如“巧克力”之类的参数调用 @candies 时,我得到了 600 个左右的结果。它已成功过滤!
But when I download @candies using axlsx using:但是当我使用 axlsx 下载 @candies 时:
//index.xlsx.axlsx
require 'axlsx'
xlsx_package = Axlsx::Package.new
workbook = xlsx_package.workbook
workbook.add_worksheet(name: "Candies") do |sheet|
sheet.add_row ["id", "name", "type", "date"]
@candies.each do |candy|
sheet.add_row [candy.id, candy.name, candy.type, candy.date]
end
end
It generates file with all 600 records!它生成包含所有 600 条记录的文件!
This question very similar to Ransack Search Results - to_xls?这个问题与Ransack Search Results - to_xls非常相似? However I've encountered same problem using axlsx gem instead of to_xls gem!
但是我在使用 axlsx gem 而不是 to_xls gem 时遇到了同样的问题!
your search result should be without "all"您的搜索结果应该没有“全部”
@q = Candy.ransack(params[:q])
@candies = @q.result
put "total record = ", @candies.count
I add trace method, so you can trace the result, you can check the result from rails server console / development log file我添加了trace方法,所以你可以跟踪结果,你可以从rails服务器控制台/开发日志文件中查看结果
Then make sure that you have permitted the relevant parameter in your controller.然后确保您已允许控制器中的相关参数。 Given we are not changing any fields, we can safely do so:
鉴于我们没有更改任何字段,我们可以安全地这样做:
def search
params.permit![:format] # must permit this
@q = Shift.ransack(params[:q])
@shifts = @q.result
respond_to do |format|
format.xlsx
format.html
end
end结束
@q = Candy.ransack(params[:q])
@candies = @q.result.page
puts "total record = #{@candies.count}"
I think you have set a page limit to display on UI.我认为您已经设置了在 UI 上显示的页面限制。 Use .page to filter and send the same var to axslx file.
使用 .page 过滤并将相同的 var 发送到 axslx 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.