title: How to connect a file_field to the upload action in a rails app
body: <p>I'm trying to add to a very simple data entry form, a button that allows the user to upload a file to the servers file system so that the url to that file can be placed in a field of the database.</p>
  
  <p>So far, I've worked out that in the _form...erb file, I can add the file_field helper:</p>
  
  <pre><code>&lt;%= form_for(@business) do |f| %&gt;
  &lt;% if @business.errors.any? %&gt;
  &lt;div id="error_explanation"&gt;
  &lt;h2&gt;&lt;%= pluralize(@business.errors.count, "error") %&gt; prohibited this business from being saved:&lt;/h2&gt;
  
  &lt;ul&gt;
  &lt;% @business.errors.full_messages.each do |msg| %&gt;
  &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
  &lt;% end %&gt;
  &lt;/ul&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;div class="field"&gt;
  &lt;%= f.label :deletedFlag %&gt;&lt;br /&gt;
  &lt;%= f.check_box :deletedFlag %&gt;
  &lt;/div&gt;
  &lt;div class="field"&gt;
  &lt;%= f.label :name %&gt;&lt;br /&gt;
  &lt;%= f.text_field :name %&gt;
  &lt;/div&gt;
  
  ...
  
  &lt;div class="field"&gt;
  &lt;%= f.label :logoURL %&gt;&lt;br /&gt;
  &lt;%= f.text_field :logoURL %&gt;
  
      &lt;%= f.file_field :imagefile %&gt;
  &lt;/div&gt;
  
  &lt;div class="field"&gt;
  &lt;%= f.label :streetAddress %&gt;&lt;br /&gt;
  &lt;%= f.text_area :streetAddress %&gt;
  &lt;/div&gt;
  &lt;div class="actions"&gt;
  &lt;%= f.submit %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>This gives me a form with a "Choose file" button.  Clicking on that allows the selection of the file.  Great.</p>
  
  <p>Now, from the ruby guide at: <a href="http://guides.rubyonrails.org/form_helpers.html#uploading-files" rel="nofollow">http://guides.rubyonrails.org/form_helpers.html#uploading-files</a> I'm told to add something like:</p>
  
  <pre><code>def upload
    uploaded_io = params[:business][:imagefile]
    File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
      file.write(uploaded_io.read)
    end
  end
  </code></pre>
  
  <p>to my controller.</p>
  
  <p>What I haven't worked out is how that file_field gets connected to that 'upload' action.  I can see in the logs that the button press accepts the filename, etc, but there's no indication that the file upload is started or attempted.</p>
  
  <p>I've read a number of references, and they all seem to say that you need these two things, but (for me) they don't connect the dots.  Yes I'm very new to Ruby/Rails, so I'm obviously missing something fundamental.</p>