Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Continuing from <strong>Thomas Watsons</strong> great start to the answer which helped me get my bearings before figuring the rest of it out.</p> <p>The code blow allows not just CSV upload for the example Posts model but for any subsequent models thereafter. all you need to do is copy the action_item ands both collection_actions from the example into any other ActiveAdmin.register block and the functionality will be the same. hope this helps.</p> <p><strong>app/admin/posts.rb</strong></p> <pre><code>ActiveAdmin.register Post do action_item :only =&gt; :index do link_to 'Upload CSV', :action =&gt; 'upload_csv' end collection_action :upload_csv do render "admin/csv/upload_csv" end collection_action :import_csv, :method =&gt; :post do CsvDb.convert_save("post", params[:dump][:file]) redirect_to :action =&gt; :index, :notice =&gt; "CSV imported successfully!" end end </code></pre> <p><strong>app/models/csv_db.rb</strong></p> <pre><code>require 'csv' class CsvDb class &lt;&lt; self def convert_save(model_name, csv_data) csv_file = csv_data.read CSV.parse(csv_file) do |row| target_model = model_name.classify.constantize new_object = target_model.new column_iterator = -1 target_model.column_names.each do |key| column_iterator += 1 unless key == "ID" value = row[column_iterator] new_object.send "#{key}=", value end end new_object.save end end end end </code></pre> <p><em>note: this example does a check to see whether or not the first column is an ID column, it then skips that column as rails will assign an ID to the new object (see example CSV below for reference)</em></p> <p><strong>app/views/admin/csv/upload_csv.html.haml</strong></p> <pre><code>= form_for :dump, :url=&gt;{:action=&gt;"import_csv"}, :html =&gt; { :multipart =&gt; true } do |f| %table %tr %td %label{:for =&gt; "dump_file"} Select a CSV File : %td = f.file_field :file %tr %td = submit_tag 'Submit' </code></pre> <p><strong>app/public/example.csv</strong></p> <pre><code>"1","TITLE EXAMPLE","MESSAGE EXAMPLE","POSTED AT DATETIME" "2","TITLE EXAMPLE","MESSAGE EXAMPLE","POSTED AT DATETIME" "3","TITLE EXAMPLE","MESSAGE EXAMPLE","POSTED AT DATETIME" "4","TITLE EXAMPLE","MESSAGE EXAMPLE","POSTED AT DATETIME" "5","TITLE EXAMPLE","MESSAGE EXAMPLE","POSTED AT DATETIME" </code></pre> <p><em>note: quotations not always needed</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload