如何在新页面中显示 rails 字符串变量并在 Rails 中另存为 csv 文件

How to display rails string variable in a new page and save as csv file in Rails

我在 Rails 上使用 Ruby,我想在用户单击当前页面中的按钮时在新页面中显示一些字符串。

我将在新页面中显示的字符串存储在名为“@csv”的变量中。我从我的控制器得到了这个@csv 变量。

目前,我的代码显示一个 table 包含 csv 的内容和一个按钮。当用户单击该按钮时,应在其浏览器中显示一个新页面,显示 csv 字符串(table 的内容)并允许用户将其下载为 csv 文件。

我希望它像 chrome 中的那些 pdf 文件一样工作。我们知道,当我们在 chrome 中单击一个 pdf 文件时,chrome 将在新选项卡中显示这些 pdf 文件,并允许用户在按下 control + s 时下载它。这就是我想要的。但问题是我所拥有的只是 index.html.erb 页面中的一个 @csv 变量,而不是 csv 文件。

我的代码在这里:

index.html.rb(在 'table' 文件夹内)

<h1>Search Results</h1>
<table class="table table-striped table-bordered">

    <% @csv.each_line do |line| %>
        <tr>
            <% line.split(",").each do |f| %>
                <th><%= f %></th>
            <% end %>
        </tr>
    <% end %>

</table>

<form action="/export.html" method="get" target="_blank">
    <input type="submit" class="btn btn-success" value="Export as CSV" >
</form>

table_controller.rb

class TableController < ApplicationController

    def index
        #Get the search terms first and store it in a variable
        @q = params[:q]
        #create a file that stores the search terms
        fileName = "fileName"   #must be named fileName because I don't know how to pass parameter using %x
        out_file = File.new(fileName, "w")
        out_file.puts(@q)
        out_file.close

        #an executable file named SearchEngine will:
        #1) take a parameter containing the name of the file that contains a string search terms (example: Women over 50)
        #2) stdout (print to terminal) a csv file (line by line, comma separated lines)
        #then I will store the output of the executable in my @csv variable so that I can use it to display the search resultin the table in index.html.rb page
        @csv = %x(SearchEngine fileName)

        #finished using the search term file so I can delete it now
        File.delete(fileName)
    end

end

如果需要更多信息或我的问题不清楚,请告诉我。

你说的是内联处理。在 Rails.

中很容易实现

您已经有了带有 csv 的字符串,因此您只需将其正确发送到浏览器即可。

def index
  # ...
  @csv = %x(SearchEngine fileName)
  File.delete(fileName)

  respond_to do |format|
    format.html
    format.csv do
      send_data @csv, type: Mime::CSV, disposition: 'inline'
    end
  end
end

之后,您可以通过将 .csv 附加到 url 来在浏览器中访问 c​​sv。

如果您需要在不同的地方重复相同的过程,那么您可以更轻松地为 csv 创建控制器渲染器并在任何地方使用它。

# config/initializers/controller_renderers.rb
ActionController::Renderers.add :csv do |obj, options|
  filename = options[:filename] || 'data'
  disposition = options[:disposition] || 'inline'
  str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
  send_data str, type: Mime::CSV, disposition: disposition,
    filename: "#{filename}.csv"
end

# inside your controller action
respond_to do |format|
  format.html
  format.csv { render csv: @csv, filename: "mycsv" }
end

单击查看页面上的导出为 CSV 按钮后,我现在可以下载 csv 文件。但我不知道为什么那个 csv 文件没有显示在新页面上。我使用 inline 进行处理,因此它应该在新页面上显示 csv。我不知道为什么它不这样做。

但是,对于有兴趣从变量下载 csv 文件的人来说,新文件如下所示:

table_controller.rb

class TableController < ApplicationController
    def index
        @q = params[:q]
        fileName = "fileName"
        out_file = File.new(fileName, "w")
        out_file.puts(@q)
        out_file.close

        @csv = %x(SearchEngine fileName)

        File.delete(fileName)
        respond_to do |format|
            format.html
            format.csv do
                send_data @csv, type: Mime::CSV, disposition: 'inline', filename: 'csv_search_results.csv'
            end
        end
    end
end

index.html.rb(在 'table' 文件夹内)

<h1>Search Results</h1>
<table class="table table-striped table-bordered">

    <% @csv.each_line do |line| %>
        <tr>
            <% line.split(",").each do |f| %>
                <th><%= f %></th>
            <% end %>
        </tr>
    <% end %>

</table>

<%= link_to "Export as CSV", {:action => "index", :format => :csv}, :target => '_blank' %>