2017-12-29 39 views
0

그래서 Ruby on Rails 웹 사이트를 개발하여 웹 사이트에 Excel 파일을 가져옵니다. 나는 RailsCasts을 따라 갔고 Roo gem을 사용했다. 모두 작동하지만 가져 오기 프로세스가 완료되면 모든 입력란은 nil입니다. raise으로 확인할 때 데이터가 없음을 알았습니다.Ruby on Rails Roo 가져 오기 Excel 반환 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 

row을 올렸을 때 Excel에서 전체 데이터를 받았습니다. 그러나 내가 product을 키 웠을 때, 나는 아무 것도 얻지 못했다. 어떻게 모든 값을 올바르게 얻을 수 있도록 데이터를 올바르게 넣을 수 있습니까? 고맙습니다.

답변

0

일부 연습을 한 후에 첫 번째 테스트에서는 해시이므로 실제로 작동시키기 위해 필요한 것은 사실 간단하지만 이것이 최상의 방법인지는 확실하지 않습니다. 누군가가 더 나은 실천을한다면, 그것은 좋은 일이 될 것이지만, 이것은 현재 나의 해결책입니다.

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'] 
     )