2017-12-29 18 views
0

Also, ich entwickle eine Ruby on Rails-Website, um eine Excel-Datei in unsere Website zu importieren. Ich folgte RailsCasts und mit Roo gem. Alle funktionieren, aber wenn der Importvorgang abgeschlossen ist, sind alle Felder gleich Null. Als ich mit raise überprüfte, fand ich heraus, dass die Daten null sind.Ruby on Rails Roo Importieren Excel Zurück NIL

def self.import(file) 
    spreadsheet = open_spreadsheet(file) 
    header = spreadsheet.row(1) 
    (2..spreadsheet.last_row).each do |i| 
     row = Hash[[header, spreadsheet.row(i)].transpose] 
     product = find_by_id(row["id"]) || new 
     product.attributes = row.to_hash.slice(*accessible_attributes) 
     product.save! 
    end 
    end 

Als ich row angehoben habe ich ganze Daten aus Excel. Aber als ich product angehoben habe, habe ich null. Wie kann ich die Daten richtig setzen, damit ich alle Werte richtig bekommen kann? Vielen Dank.

Antwort

0

Also, nach einigen Übungen, fand ich heraus, dass es beim ersten Test ein Hash war, also was ich brauche, um es zum Laufen zu bringen, ist eigentlich einfach, aber ich bin mir nicht sicher, ob dies die beste Praxis ist. Wenn jemand eine bessere Übung hat, wird es eine gute Sache sein, aber das ist meine Lösung für jetzt.

object = Lead.new(
     :first_name => row['First Name'], :last_name => row['Last Name'], 
     :address => row['Address'], :city => row['City'], 
     :state => row['State'], :county => row['County'], 
     :zip_code => row['Zip Code'], :bedrooms => row['Bedrooms'], :bathrooms => row['Bathrooms'], 
     :last_sale_date => row['Last Sale Date'], :amount_sold => row['Amount Sold'], 
     :tax_value => row['Tax Value'], :tax_name => row['Tax Name'], :tax_address => row['Tax Address'], 
     :tax_city => row['Tax City'], :tax_state => row['Tax State'], :tax_postal_code => row['Tax Postal Code'], 
     :listing_status => row['Listing Status'], :property_type => row['Property Type'], 
     :square_footage => row['Square Footage'], :days_on_market => row['Days on Market'], :list_date => row['List Date'], 
     :status_change_date => row['Status Change Date'], :year_built => row['Year Built'], 
     :mls_id => row['MLS ID'], :last_call_result => row['Last Call Result'], :last_dial_date => row['Last Dial Date'], 
     :last_contacted => row['Last Contacted'], :last_dial_time => row['Last Dial Time'], :create_date => row['Create Date'], 
     :edit_date => row['Edit Date'], :source => row['Source'], :list => row['List'], 
     :call_attempts => row['Call Attempts'], :family_member => row['Family Member'], :notes => row['Notes'], 
     :group => row['Group'], :manager => row['Manager'] 
     )