Rails Controller - Cant load such file (CSV) - ruby-on-rails

I have the following in my view:
<%= form_tag import_list_path, multipart: true do %>
<%= file_field_tag(:file) %>
<%= submit_tag(:Submit) %>
<% end %>
I have this in my controller:
def import
require 'csv'
csv = CSV.load params[:file].tempfile.read
CSV.new(csv.tempfile, :col_sep => ",", :return_headers => false).each do |column|
name_array << column[5]
end
redirect_to(:index)
end
I'm just trying to store a temporary CSV file in memory and do some actions on it, essentially using it to pull in information to be used in consuming a web service later.
This is the error I receive:
cannot load such file -- Column1,Column2,Column3,Column4,Column5,Column6,Column7,etc....
How can I change my controller to not throw this error?

That should do it.
def import
require 'csv'
CSV.new(params[:file].tempfile, :col_sep => ",", :return_headers => false).each do |column|
name_array << column[5]
end
redirect_to(:index)
end
Another notice: Dont put your logic in your controller it belongs in the model ;)
//
That menas you should write a method in your model that deals with the data and only refere the path to the csv file as a parameter of the method. The model is there as an interface between you app and the database and for the things done in the app. The View is there to display your stuff and the controller is the thing that connects both.

Related

rails import csv data from dynamic url

I'm completely newbie with RoR. What I'm trying to do is to import data from an asp url:
I've a ticket that passed to this url it returns with some informations like time_created, time_updated, owner, notes. I've tried putting this code in my view:
<% file = open("http://xxx.xxx.xxx/utility/ticket_dettagli.asp?TT="+#Ticket.ticket) %>
<%= CSV.new(file, col_sep: ',').readlines do |row| %>
<%= Ticket.create! row.to_hash %>
<% end %>
without success...any suggestion?
Why are you trying to do this in view? I think, there should be rake task for import tickets form csv or class method in your model ...
See: http://railscasts.com/episodes/396-importing-csv-and-excel
#Ticket - is this a variable? How you declare it?

Uploading a File on aWebsite using Ruby Rails 4

I am using rails 4 and I have this code.
I created a controller and called it upload (upload_controller.rb)
I put this code in it:
class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.rhtml.erb'
end
def uploadFile
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
end
My model was called data_file.rb. The code is the following:
class DataFile < ActiveRecord::Base
attr_accessor :upload
def self.save(upload)
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
end
end
My view file was called uploadFile.html.erb. My code is the following:
<h1>File Upload</h1>
<%= form_tag({:action => 'uploadFile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label>
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>
My main goal of having this code is so that the user can upload a file to a specified location. The file type has to be anything not just images but doc, excel sheets, etc. Once I write this code and I do bundle install and then I do rake db:migrate and I go to tools and run the development. Once I do that I go to firefox and type in localhost:3000 and the page cannot show. (directs me to yahoo search or whatever).
I don't know what I am doing wrong here. Am I suppose to add a gem or change a certain line or something? I have been stuck on this issue for days now and I just want to move on to the next part of my webpage. Please help me. Thank you.

Rails 4 - Delayed_Job for CSV import

I'm building a marketplace app in Rails 4 where sellers can list items to sell. I have a csv import feature so sellers can bulk load products. The import code worked fine on small files but I ran into timeout issues with larger files. So I want to use delayed_job to process these files in the background.
I set up delayed_job up to the point where the job is queued (i see the job in the delayed_job table). But when I run the job, I get an error saying that the file to be imported is not found. It is looking for the file in a temp folder which doesn't exist when the job is run.
How do I save (or without saving) the file in a location where delayed_job can access it? And how to I tell delayed_job where the file is located?
my listings controller:
def import
Listing.import(params[:file], params[:user_id])
redirect_to seller_url, notice: "Products are being imported."
end
my listing model:
class Listing < ActiveRecord::Base
require 'csv'
require 'open-uri'
class << self
def importcsv(file_path)
CSV.foreach(file_path, headers: true, skip_blanks: true) do |row|
#some model processing
end
end
handle_asynchronously :importcsv
end
# My importer as a class method
def self.import(file, user_id)
Listing.importcsv file.path
end
end
Here is form view:
<%= form_tag import_listings_path, multipart: true do %>
<%= file_field_tag :file %>
<%= hidden_field_tag :user_id, current_user.id %>
<%= submit_tag "Import CSV" %>
<% end %>
Presumably the file is a form upload. I think those files only persist while the web request is running. My recommendation is to use FileUtils.copy to copy the file into some location that will exist when your job runs.
So, probably you don't want to handle_asynchronously importcsv, but instead copy the files then call a private method on your model (which will be handled asynchronously) with the new file paths.

Reading in file contents rails

I have a form that is attempting to read in a JSON file for parsing/actions/etc. I'm having problems getting it to read in the controller.
View:
<%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post}) do |f| %>
<%= file_field_tag 'datafile' %>
<%= submit_tag "Upload" %>
Controller:
def upload_json
file_data = params[:datafile]
File.read(file_data) do |file|
file.each do |line|
## does stuff here....
end
end
end
A similar function works in my seed.rb file when I'm seeding data - just can't get it to read in an uploaded file.
The error I'm getting is: can't convert ActionDispatch::Http::UploadedFile into String.
Thanks in advance for the help!
Figured it out. Needed to change:
file_data = params[:datafile]
to
file_data = params[:datafile].tempfile
And decided to use the .open function to change:
File.read(file_data) do |file|
to
File.open(file_data, 'r') do |file|
params[:datafile] is an instance of ActionDispatch::Http::UploadedFile class with tempfile attached with that.To open the tempfile
You try something like
File.open(params[:datafile].path) do |file|
#your stuff goes here
end
Open the uploaded file using path.
params[:datafile] is an instance of ActionDispatch::Http::UploadedFile class and you'll need to get at the stored file by calling path to properly process it.
Additionally, File.read will not get you the line-by-line processing you're looking for. You need to change that to File.open.
Try this:
Controller
def upload_json
uploaded_datafile = params[:datafile]
File.open( uploaded_datafile.path ) do |file|
file.each_line do |line|
# Do something with each line.
end
end
end
Alternative Style
def upload_json
File.foreach( params[:datafile].path ) do |line|
# Do something with each line.
end
# FYI: The above method block returns `nil` when everything goes okay.
end

Rails - CSV export: prompt for file download

I want to give my users the ability to export a table to CSV.
So in my controller, I've added on top of the file:
respond_to :html, :js, :csv
I'm also setting the headers if the requested format is csv:
if params[:format] == 'csv'
generate_csv_headers("negotiations-#{Time.now.strftime("%Y%m%d")}")
end
Code for generate_csv_headers(in application_controller) is:
def generate_csv_headers(filename)
headers.merge!({
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=\"#{filename}\"",
'Content-Transfer-Encoding' => 'binary'
})
end
I've also created a view named index.csv.erb to generate my file:
<%- headers = ["Id", "Name"] -%>
<%= CSV.generate_line headers %>
<%- #negotiations.each do |n| -%>
<%- row = [ n.id,
n.name ] -%>
<%= CSV.generate_line row %>
<%- end -%>
I don't have any error, but it simply displays the content of the CSV file, while I'd expect a prompt from the browser to download the file.
I've read a lot, but could not find anything that'd work.
Do you have an idea?
thanks, p.
I'm still unsure about why this fixed the issue, but it did.
I changed the link in the view to
<%= link_to "Export to csv", request.parameters.merge({:format => :csv})%>
and it now works!
I'm not sure you have to do it this way, but if you generate and save the file, you can use send_file to send it to the browser. See http://api.rubyonrails.org/classes/ActionController/Streaming.html
You might be interested in this gem I made called CSV shaper that allows you to create CSV output using a really nice Ruby DSL.
It will also handle setting the response headers correctly, while allowing filename customisaton.
https://github.com/paulspringett/csv_shaper

Resources