Is there any way to Take the full path to import a csv File?

Asked

Viewed 94 times

0

As described in the title of my question, I would like to know if you have any way to get the path to a csv file, in case I need this path to automatically import the file.

This is my attempt to do this. form

<%= form.file_field :file %>

This input I’m using to get the file and the symbol :file as parameter to return the path.

users_controller

def create
@user = User.new(user_params)
binding.pry

require 'csv'

CSV.foreach(@user.file, col_sep: ',').with_index do |l, i|
  unless (indice == 0)
    User.create!(name: l[0], email: l[1], address: l[2])
  end
end

respond_to do |format|
  if @user.save
    format.html { redirect_to @user, notice: 'User was successfully created.' }
    format.json { render :show, status: :created, location: @user }
  else
    format.html { render :new }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

end

I used Binding.Pry to show the result that is returning me, and also the reason for my question. When I go to check the parameters that came to the user_params this type of information is shown:

<ActionController::Parameters {"file"=>#<ActionDispatch::Http::UploadedFile:0xb454db98 @tempfile=#<Tempfile:/tmp/RackMultipart20190710-3063-2ah7nt.csv>, @original_filename="users.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"user[file]\"; filename=\"users.csv\"\r\nContent-Type: text/csv\r\n">} permitted: true>

OK. Now I’ll show you what’s inside the parameter file when I execute @user.file on the Pry console.

"#<ActionDispatch::Http::UploadedFile:0xb454db98>"

All right, I’d like to know if you have a way to return the path to the file or convert this "#<ActionDispatch::Http::UploadedFile:0xb454db98>" on the way to the archive.

1 answer

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.