I hit a roadblock here and need help. I want to be able to import csv file to my Active Record. Either with SmarterCSV or some other way
Here is my database
create_table "ques", force: true do |t|
t.integer "rikt_nr"
t.integer "start_nr"
t.integer "end_nr"
t.datetime "created_at"
t.datetime "updated_at"
end
here is my view
<h2>Import Ques</h2>
<%= form_tag import_ques_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
here is my route
resources :ques do
collection { post :import }
end
root to: 'ques#index'
and my controller
def import
Que.import(params[:file])
redirect_to root_url, notice: "Ques imported."
end
and the model
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Que.create! row.to_hash
end
end
and the csv file is looking like this
Id;Rikt nr;Start nr;End nr;Created at;Updated at
1;8;4486550;4486650;October 28, 2014 08:42;October 28, 2014 08:42
2;8;4486700;4486755;October 28, 2014 08:42;October 28, 2014 08:42
I have looked at all sort of guides, but I just cant get it to work.
First: you don't use
smarter_csv
in your example, but standard Ruby CSV.Second:
smarter_csv
gives you options to specify the delimiting character but doesn'tauto detect
it.in conclusion, you
import
should look likeFor anybody else that is having a problem with this. This is how I solved it. First off change the csv file to have "," as the delimiters
Then use the code I have, but change the controller to this
and it works