Forum: Ruby on Rails using import CSV with params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
denver (Guest)
on 2008-12-03 16:45
(Received via mailing list)
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| %>
  <p>
    <%= f.label :file, 'Import mail list file' %><br/>
    <%= f.file_field :file -%>
  </p>
  <p>
    <%= submit_tag "Submit" %>
  </p>
<% end %>

-------------------------------------------------------------------------------------------------------------------------------
Thorsten M. (Guest)
on 2008-12-03 17:30
(Received via mailing list)
if I get this right, in this line:
Mailing.create!(row.to_hash)
you want to add the :malinglist_id from params[:id]

that should work:
Mailing.create!(row.to_hash.merge!({:malinglist_id => params[:id]}))
Bill W. (Guest)
on 2008-12-03 18:00
(Received via mailing list)
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
> 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 %>
>
> --------------------------------------------------------------------------
-----------------------------------------------------
This topic is locked and can not be replied to.