urco
urco

Reputation: 189

Upload file, not such file or directory - rails 4

I have this in the index (pieces_path)

<%= form_tag (upload_pieces_path) do%>
  <%= file_field_tag 'data' %>
  <%= submit_tag %>
<% end %>

And this in the pieces controller

def upload
  file_data = params[:data]
  File.read(file_data, 'r') do |file|

     file.each do |line|
       ## .... ##
     end

   end
   redirect_to pieces_path
end

Finally the routes

match '/upload_pieces', to: 'pieces#upload', via: 'post'

The idea is to read a file in the view, then do something with him ,not save it in the database, then return to the previous page.

But when i test it i receive a "No such file or directory" error when i upload a file.

Any ideas?, what i am doing wrong?

Upvotes: 0

Views: 670

Answers (1)

markets
markets

Reputation: 7043

You should add multipart option to form_tag:

<%= form_tag(upload_pieces_path, multipart: true) do %>
  <%= file_field_tag 'data' %>
  <%= submit_tag %>
<% end %>

Upvotes: 5

Related Questions