Home>

I want to output a CSV file narrowed down by conditions with Rails

The CSV output function is implemented in Rails by referring to this web page.

[CSV.generate] How to output CSV file with Rails

The problem i am having

Form values ​​cannot be submitted as URL parameters using the link to and GET methods from View.

Corresponding source code

Above page, of this code@users = User.allPart of@users = User.Where (name: params [: name])As
I want to create a CSV file that is narrowed down by conditions according to the parameters passed from view.

require'csv'
class UsersController</pre>
<p>Place the line of link_to ~ on the View side in the file created by form_with, and use the form input value (@name) as the URL parameter.<br />
To be able to send<code>name: # {@name}</code>I wrote as, but the value cannot be passed (it will be empty)<br />
*<code>name: "hogehoge"</code>If i enter the value directly, the parameter will be generated.</p>
<p>Is this description incorrect?</p>
<pre><code><% = link_to "CSV output", users_path (format:: csv, name: # {@ name})%>

Postscript: Code on the view side (_form.html.erb)

<section>
<% = form_with url: files_path, method:: get, local: true do | form |%>
  
    
    <% = form.label: Name%>
    <% = form.text_field: name, class: "form-control", placeholder: "name"%>
    
  
  <% = link_to "CSV output", files_path (format:: csv, name: # {@ name}), class: "btn btn-primary"%>
  
<% end%>
</section>