簡體   English   中英

如何在Ruby on Rails中將GET參數傳遞給下一個控制器動作?

[英]How to pass GET parameters to next controller action in Ruby on Rails?

在我的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") %>

但是,它會下載所有發票,而不僅僅是用戶以前過濾掉的發票。

有沒有辦法將搜索參數傳遞給下一個動作,或者也許是一個更優雅的解決方案?

謝謝你的幫助。

您只需要發送參數以及format

這將達到目的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM