Hi denver, denver wrote:
> 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!! The easiest way might be to add a hidden field to your form to pass the controller the mailinglist_id. I've added a little code below (untested) that might do the trick. HTH, Bill > Database table "mailings" is as follows... > > mailinglist_id, id, name, email_address > > -------------------------------------------------------------------------- --------------------------------------------------- > import_csv controller > > def csv_import * mailing_list_id = params[:mailing_list_id] > file = params[:csv_import][:file] > logcount=0 > Mailing.transaction do > FasterCSV.parse(file, :headers => true) do |row| > Mailing.create!(row.to_hash) * Mailing.mailing_list_id = mailing_list_id * Mailing.save > 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| %> > <p> > <%= f.label :file, 'Import mail list file' %><br/> > <%= f.file_field :file -%> * <%= hidden_field_tag('mailing_list_id', 'a local or an instance variable') %> > </p> > <p> > <%= submit_tag "Submit" %> > </p> > <% end %> > > -------------------------------------------------------------------------- ----------------------------------------------------- > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---