, , , . , .
routes.rb
resources :contacts do
collection do
get 'import/new', to: :new_import
post :import, on: :collection
end
end
//new_import.html.erb
<%= form_for @contacts, url: import_contacts_path, html: { multipart: true } do |f| %>
<%= f.file_field :import_file %>
<% end %>
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|
end
end
Hope this helps others!
In JP
source
share