using import CSV with params

denver
Wed, 03 Dec 2008 14:44:20 -0000

Hi Guys,

I am using import CSV to import from a csv file to my database in my
rails app. I want to be able to be able to add a list of email
addresses from this file but also include the mailinglist_id which is
a param[:id] on my page in the database entry. So in effect adding a
csv of emails to any mailing list.

I am just a little unclear as to how I add the extra field when adding
the data to the database.... any help would be great!!

Database table "mailings" is as follows...

mailinglist_id, id, name, email_address

-----------------------------------------------------------------------------------------------------------------------------
import_csv controller

 def csv_import
      file = params[:csv_import][:file]
      logcount=0
      Mailing.transaction do
        FasterCSV.parse(file, :headers => true) do |row|
          Mailing.create!(row.to_hash)
          logcount += 1
        end
      end
      flash[:notice] = "Successfully imported #{logcount} email(s)
from mailing list."
      redirect_to :action => :index
    rescue => exception
      # If an exception is thrown, the transaction rolls back and we
end up in this rescue block
      error = ERB::Util.h(exception.to_s) # get the error and HTML
escape it
      flash[:error] = "Error adding list.  (#{error}).  Please try
again."
      redirect_to :action => :index
    end
-----------------------------------------------------------------------------------------------------------------------------

index.html.erb

<% form_for :csv_import, :url=>{ :controller=>"import_csv",
:action =>
''csv_import''},
:html => { :multipart => true } do |f| %>

		<%= f.label :file, ''Import mail list file''
%>
		<%= f.file_field :file -%>


		<%= submit_tag "Submit" %>

<% end %>

-------------------------------------------------------------------------------------------------------------------------------