title: Read uploaded JSON file in Rails
body: <p>I'm uploading a JSON file in Rails in order to parse it and then insert into the database without storing the file itself</p>
  
  <p>Form</p>
  
  <pre><code>= form_tag import_posts_path do
    = file_field_tag :file
    = submit_tag "import"
  </code></pre>
  
  <p>Controller method</p>
  
  <pre><code>def import
    file = params[:file]
  
    puts "file =&gt; " + file # file =&gt; posts.json
  
    data = File.read(params[:file]) # Errno::ENOENT at /posts/import No such file or directory @ rb_sysopen - posts.json
  
    json = JSON.parse(data)
  end
  </code></pre>
  
  <p>routes</p>
  
  <pre><code>resources :posts do
    collection { post :import }
  end
  </code></pre>
  
  <p>request info in Terminal, and the <code>posts.json</code> is there but I get <code>Errno::ENOENT at /posts/import No such file or directory @ rb_sysopen - posts.json</code></p>
  
  <pre><code>  Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"9lzpwyCWclgiI4WA/zho5y1WLCBQtRL7z3FcAn+wc80=", "file"=&gt;"posts.json", "commit"=&gt;"import"}
  file =&gt; posts.json
  Completed 500 Internal Server Error in 23ms
  </code></pre>