Step:1
Add the following line in application.rb file
require 'csv'
Restart the server.
Step:2
Add the block in your view page for users to upload CSV file.
<%= form_tag home_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
Step:3
Add the block in your controller
def import
Users.import(params[:file])
end
Step:4
Go to user model and write the following code block
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Users.create! row.to_hash
end
end
Comments
Post a Comment