title: I want to pass a variable from file_field_tag to a rake task via the controller in Rails
body: <p>I want to pass the filename (<code>health_exported_zip</code>) to a controller action then on to a rake task. The rake task imports selected data from <code>export.zip</code> into the database and the rake task works if I hard code <code>health_exported_zip</code> into the rake task. But I can't get the variable from the page to the controller, nor from the controller to the rake task.</p>
  <p><code>index.html.erb</code></p>
  <pre><code>&lt;%= form_tag( action: :import_data, controller: :some_models ) do %&gt;
    &lt;%= file_field_tag :health_exported_zip %&gt;
    &lt;%= submit_tag( &quot;Import&quot; ) %&gt;
  &lt;% end %&gt;
  </code></pre>
  <p><code>some_models_controller.rb</code></p>
  <pre><code>def import_data
    health_exported_zip = @health_exported_zip
    puts &quot;health_exported_zip: #{health_exported_zip}. Selected&quot;
    @health_exported_zip = &quot;/Users/gscar/Downloads/export.zip&quot;
    puts &quot;@health_exported_zip: #{@health_exported_zip}. Hard coded in controller&quot;
    system &quot;rails import:some_models[#{@health_exported_zip}]&quot;
    ...
  end
  </code></pre>
  <p><code>lib/tasks/import.rake</code></p>
  <pre><code>...
  extract_zip(health_exported_zip, unzip_to_dir) # line 49
  ...
  </code></pre>
  <p>Console:</p>
  <pre><code>Started POST &quot;/import_data&quot; for ::1 at 2021-11-11 21:24:24 -0800
  Processing by SomeModelsController#import_data as TURBO_STREAM
    Parameters: {&quot;authenticity_token&quot;=&gt;&quot;[FILTERED]&quot;, &quot;commit&quot;=&gt;&quot;Import&quot;}
  health_exported_zip: . Selected
  @health_exported_zip: /Users/gscar/Downloads/export.zip. Hard coded in controller
  rails aborted!
  NameError: undefined local variable or method `health_exported_zip' for main:Object
  ~/lib/tasks/import.rake:49:in `&lt;main&gt;'    
  </code></pre>
  <p>Thanks for any pointers.</p>
  <p>PS. One old Rails 3 suggestion</p>
  <blockquote>
  <p>rake tasks are not intended to be run from a controller. Consider
  refactoring and extracting take task functionality to separate method.
  Then you can call this method both from the rake task and the
  controller action.</p>
  </blockquote>
  <p>Assume this still holds and I can do that when I get it working.</p>