Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. # In new.html.erb
  2. <%= file_field_tag 'upload[file]' %>
  3.  
  4. # In controller#create
  5. @text = params[:upload][:file]
  6.  
  7. <%= form_tag 'controller/method_name', :multipart => true do %>
  8. <label for="file">Upload text File</label> <%= file_field_tag "file" %>
  9. <%= submit_tag %>
  10. <% end %>
  11.  
  12. uploaded_file = params[:file]
  13. file_content = uploaded_file.read
  14. puts "file_content"
  15.  
  16. resources :contacts do
  17. collection do
  18. get 'import/new', to: :new_import # import_new_contacts_path
  19.  
  20. post :import, on: :collection # import_contacts_path
  21. end
  22. end
  23.  
  24. <%= form_for @contacts, url: import_contacts_path, html: { multipart: true } do |f| %>
  25.  
  26. <%= f.file_field :import_file %>
  27.  
  28. <% end %>
  29.  
  30. def new_import
  31. end
  32.  
  33. def import
  34. begin
  35. Contact.import( params[:contacts][:import_file] )
  36.  
  37. flash[:success] = "<strong>Contacts Imported!</strong>"
  38.  
  39. redirect_to contacts_path
  40.  
  41. rescue => exception
  42. flash[:error] = "There was a problem importing that contacts file.<br>
  43. <strong>#{exception.message}</strong><br>"
  44.  
  45. redirect_to import_new_contacts_path
  46. end
  47. end
  48.  
  49. def import import_file
  50. File.foreach( import_file.path ).with_index do |line, index|
  51.  
  52. # Process each line.
  53.  
  54. # For any errors just raise an error with a message like this:
  55. # raise "There is a duplicate in row #{index + 1}."
  56. # And your controller will redirect the user and show a flash message.
  57.  
  58. end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement