Full example
Take, for example, loading an import file containing contacts. You do not need to store this import file, just process it and discard it.
Routes
routes.rb
resources :contacts do collection do get 'import/new', to: :new_import
The form
view / contacts / new_import.html.erb
<%= form_for @contacts, url: import_contacts_path, html: { multipart: true } do |f| %> <%= f.file_field :import_file %> <% end %>
controller
Controllers / contacts _controller.rb
def new_import end def import begin Contact.import( params[:contacts][:import_file] ) flash[:success] = "<strong>Contacts Imported!</strong>" redirect_to contacts_path rescue => exception flash[:error] = "There was a problem importing that contacts file.<br> <strong>#{exception.message}</strong><br>" redirect_to import_new_contacts_path end end
Contact Model
/contact.rb models
def import import_file File.foreach( import_file.path ).with_index do |line, index|
Hope this helps!
Joshua
source share