I'm trying to create a form, where I can upload an CSV file to import or to preview before import it.
In my form I have:
<%= form_for(@contact_import, :remote => true) do |f| %>
<% if @contact_import.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact_import.errors.count, "error") %> prohibited this import from completing:</h2>
<ul>
<% @contact_import.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.file_field :file %>
</div>
<%= f.submit "Import" %>
<%= f.submit "Preview", :name => 'preview' %>
and in my controller:
def create
@contact_import = ContactImport.new(params[:contact_import])
if params[:preview]
logger.debug "Let's preview the contacts:" + params.inspect
@contacts = @contact_import.update_preview
@contact_attributes = ContactImport.mapping_attributes
#I should now update the preview div
else
logger.debug("Got the commit" + params.inspect)
if @contact_import.save
redirect_to root_url, notice: "Imported contacts successfully."
else
render :new
end
end
end
How can I update the view to show the preview contacts, by uploading the CSV file?
Note: The CVS file processing at the moment is at the model and had been omitted.