但是-6ren">
gpt4 book ai didi

ruby-on-rails - 如何将 GET 参数传递给 Ruby on Rails 中的下一个 Controller 操作?

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:36 25 4
gpt4 key购买 nike

在我的 Rails 4.2 应用程序中,我有一个 index 操作,它列出了所有用户的发票,按 URL 中的搜索参数过滤:

class InvoicesController < ApplicationController

def index
@invoices = current_user.invoices.search(params)
respond_to do |format|
format.html do
@title = "Invoices"
...
end
format.csv do
headers['Content-Disposition'] = "attachment; filename=\"#{invoices_filename}.csv\""
end
format.xls do
headers['Content-Disposition'] = "attachment; filename=\"#{invoices_filename}.xls\""
end
end
end

...

end

这很好用。用户可以通过各种参数过滤他的发票,并且列出的发票会相应调整。

当用户想要以 XLS 或 CSV 格式下载这些准确的发票时,问题就开始了。我在 index.html.erb 上有一个链接:

<%= link_to "Download invoices", invoices_path(:format => "xls") %>

但是,它会下载所有发票,而不仅仅是用户之前过滤掉的发票。

有没有一种方法可以将搜索参数传递给下一个操作或者更优雅的解决方案?

感谢您的帮助。

最佳答案

您只需要将参数连同格式

一起发送

这样就可以了:

<%= link_to "Download invoices", invoices_path(params.merge(format: :xls)) %>

关于ruby-on-rails - 如何将 GET 参数传递给 Ruby on Rails 中的下一个 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528650/

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