Code Monkey home page Code Monkey logo

Comments (15)

Fivell avatar Fivell commented on August 29, 2024 1

Can you check this branch https://github.com/activeadmin-plugins/active_admin_import/tree/import_with_particular_foreign_key ?

before_batch_import: ->(importer) {
  #your manipulation with data before import 
  #.....
  importer.csv_lines.map! { |row| row << importer.model.catalog_id}
  importer.headers.merge!({:'Catalog Id' => :catalog_id}) #this line needed if you're reading CSV headers from file, otherwise just put :catalog_id at the end of :csv_headers  declarative array 
}

from active_admin_import.

Fivell avatar Fivell commented on August 29, 2024

@niksfirefly , you can add attribute to your model it will be assigned from form, and then before_batch_import gives you access to your import model.

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

Hi @Fivell, would you be able to give an example of this. I'm using a custom template for the import and I want to add two drop downs, that will populate two id fields in the table that I'm importing into. Thanks.

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

@niksfirefly, were you able to get this working? I'm trying to do the same thing...

from active_admin_import.

Fivell avatar Fivell commented on August 29, 2024

@amanders23 , do you have problems adding drop-downs or saving data?

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

Both. I can a drop down like this, but it will not save the data to the table.

<p class="active_admin_import_hint">
   <%= raw(@active_admin_import_model.hint) %> 
</p>
<%= semantic_form_for @active_admin_import_model, url: {action: :do_import}, html: {multipart: true} do |f| %>
    <%= f.inputs name: t("active_admin_import.details") do %>

    <%= select_tag 'catalog', 
      options_for_select(Catalog.all.collect{|catalog| [catalog.name, catalog.id, {:name=>catalog.name}] }, {:prompt => 'Catalog'})%>

        <%= f.input :file, as: :file %>
    <% end %>

    <%= f.actions do %>
        <%= f.action :submit, label: t("active_admin_import.import_btn"), button_html: {data: {disable_with: t("active_admin_import.import_btn_disabled")}} %>
    <% end %>
<% end %>

I can't add a drop down like this:

   <%=f.input :catalog_id, as: :select, :collection => Catal.pluck(:name, :id)%>

I get this error:

undefined method `catalog_id' for #<ActiveAdminImport::Model:0x007f91535b2660>

If I do the drop down the first way, should there be something in my active_admin_import to receive the catalog_id and place it in the table?

Here's what I have so far:

 active_admin_import :validate => true,
 :template => 'admin/course_import' ,
 headers_rewrites: { :'subjectarea_id' => :subjectarea_id, :'generaleducation_id' => :generaleducation_id,},
 before_batch_import: ->(importer) {
                subject_names = importer.values_at(:subjectarea_id)
                 # replacing subject area name with subject area id
                 subjects   = SubjectArea.where(name: subject_names).pluck(:name, :id)
                 options = Hash[*subjects.flatten] # #{"Jane" => 2, "John" => 1}
                 puts options
                 importer.batch_replace(:subjectarea_id, options)

                  ge_names = importer.values_at(:generaleducation_id)
                  # replacing general education name with general education id
                  geneds  = GeneralEducation.where(name: ge_names).pluck(:name, :id)
                  options = Hash[*geneds.flatten] # #{"Jane" => 2, "John" => 1}
                  puts options
                  importer.batch_replace(:generaleducation_id, options)

                  # catalog_names = params[:catalog_id]
 #              # replacing author name with author id
 #              catalogs  = Catalog.where(name: catalog_names).pluck(:name, :id)
 #              options = Hash[*catalogs.flatten] # #{"Jane" => 2, "John" => 1}
 #              puts options
 #              importer.batch_replace(:catalog_id, options)
 #
               },
 :template_object => ActiveAdminImport::Model.new(
 #:hint => "file will be imported with such header format: 'body','title','author'",
 :csv_headers => ["title", "code", "description", "credit", "start_date", "end_date", "local_course_id", "prefix", "section", "prerequisites", "corequisites", "books_url", "registration_url", "active", "level", "school_id", "subjectarea_id", "course_area", "generaleducation_id", "instructor", "course_method", "seats_available", "class_full"] 
 )

It works but the catalog_id field is not populate when the form is submitted.

Thanks!

from active_admin_import.

Fivell avatar Fivell commented on August 29, 2024
  1. add needed attribute to ActiveAdminImport::Model
ActiveAdminImport::Model.new(
 #:hint => "file will be imported with such header format: 'body','title','author'",
:catalog_id => nil, #<<<this

 :csv_headers => ["title", "code", "description", "credit", "start_date", "end_date", "local_course_id", "prefix", "section", "prerequisites", "corequisites", "books_url", "registration_url", "active", "level", "school_id", "subjectarea_id", "course_area", "generaleducation_id", "instructor", "course_method", "seats_available", "class_full"] 
 )

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

I added that, and it's still just a null entry into the table.

here are my parameters that are being passed:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"sh7ZY3vDoPfIdA6BNVqDZG9w/FFcxopZQK++9SitBcHeDkRAUGBclcLZK3BwUjrIFntlH7P7Ga1QvZZ+O6zxdg==", "catalog_id"=>"1", "active_admin_import_model"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007f915ec89be0 @tempfile=#Tempfile:/var/folders/28/cs7w1cgs5bq7kx1dh_7j1kn00000gn/T/RackMultipart20150317-1413-1vn4yui.csv, @original_filename="Winter 2014-15test.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name="active_admin_import_model[file]"; filename="Winter 2014-15test.csv"\r\nContent-Type: text/csv\r\n">}, "commit"=>"Import"}

should catalog_id be outside of the active_admin_import_model?

I really appreciate your help with this!

from active_admin_import.

Fivell avatar Fivell commented on August 29, 2024

@amanders23 , not it shouldn't be outside, change form to make it model's attribute

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

Sorry to be such a pain here, but I feel like I'm missing something...

Here's my form (catalog_id shows inside the active_admin_import_model)

<p class="active_admin_import_hint">
   <%= raw(@active_admin_import_model.hint) %> 
</p>
<%= semantic_form_for @active_admin_import_model, url: {action: :do_import}, html: {multipart: true} do |f| %>
    <%= f.inputs name: t("active_admin_import.details") do %>
              <%=f.input :catalog_id, as: :select, :collection => Catalog.pluck(:name, :id)%>  #this works now
               <%= f.input :file, as: :file %>
            <% end %>
    <%= f.actions do %>
        <%= f.action :submit, label: t("active_admin_import.import_btn"), button_html: {data: {disable_with: t("active_admin_import.import_btn_disabled")}} %>
    <% end %>
<% end %>

Here's my admin/courses.rb:

 active_admin_import :validate => true,
 :template => 'admin/course_import' ,
 headers_rewrites: { :'subjectarea_id' => :subjectarea_id, :'generaleducation_id' => :generaleducation_id,},
 before_batch_import: ->(importer) {
                subject_names = importer.values_at(:subjectarea_id)
                 # replacing subject area name with subject area id
                 subjects   = SubjectArea.where(name: subject_names).pluck(:name, :id)
                 options = Hash[*subjects.flatten] # #{"Jane" => 2, "John" => 1}
                 puts options
                 importer.batch_replace(:subjectarea_id, options)
                  ge_names = importer.values_at(:generaleducation_id)
                  # replacing general education name with general education id
                  geneds  = GeneralEducation.where(name: ge_names).pluck(:name, :id)
                  options = Hash[*geneds.flatten] # #{"Jane" => 2, "John" => 1}
                  puts options
                  importer.batch_replace(:generaleducation_id, options)
               },
 :template_object => ActiveAdminImport::Model.new(
 :catalog_id => nil,
 #:hint => "file will be imported with such header format: 'body','title','author'",
 :csv_headers => ["title", "code", "description", "credit", "start_date", "end_date", "local_course_id", "prefix", "section", "prerequisites", "corequisites", "books_url", "registration_url", "active", "level", "school_id", "subjectarea_id", "course_area", "generaleducation_id", "instructor", "course_method", "seats_available", "class_full"] 
 )

from active_admin_import.

Fivell avatar Fivell commented on August 29, 2024

@amanders23 , please format your message it is impossible to read

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

Sorry about that, I'm new to using this editor. I hope that is better looking. I've pulled the log out of the comment above. Let me know if you would like to see it. I can't find a good way to clean it up.

from active_admin_import.

Fivell avatar Fivell commented on August 29, 2024

@amanders23 , I'll prepare spec example for your case for future needs

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

That would be great! Thank you!

from active_admin_import.

amanders23 avatar amanders23 commented on August 29, 2024

It works! Thanks you soooo much!

from active_admin_import.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.