Guest User

Untitled

a guest
Jun 9th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. ####parse csv####
  2.  
  3. class Editor::CsvController < ApplicationController
  4. before_filter :require_author_or_editor
  5. layout 'editor'
  6. def index
  7. #just to call the index view...
  8. end
  9. def parse
  10. @parsed_file = (params[:dump][:file])
  11. n = 0
  12.  
  13. @parsed_file.each do |big_file|
  14. big_file.split("\r").each do |line|
  15. user_array = line.split(",")
  16. user = User.new
  17.  
  18. user_name = user_array[0] +" "+ user_array[1]
  19. user_pass = user_name.reverse.gsub(/\s/,'')
  20. user.password = user_pass
  21. user.password_confirmation = user_pass
  22. user.name = user_name
  23. user.login = user_name.downcase.gsub(/\s/,'')
  24. user.email = user_array[2].strip
  25.  
  26. if user.save
  27. user.activate!
  28. author = Author.new
  29. author.user_id = user.id
  30. author.short_bio = user_array[3..-1].join(",")
  31. author.save!
  32. end
  33.  
  34. if user.valid? && author.valid?
  35. n=n+1
  36. end
  37. end
  38. end
  39. flash.now[:message]="CSV Import Successful, #{n} new authors created."
  40. redirect_to '/editor/csv'
  41. end
  42.  
  43. end
Add Comment
Please, Sign In to add comment