Cannot Parse Excel Using Multi-Schema #40

Closed
fauzieuy opened this Issue Mar 28, 2013 · 2 comments

2 participants

@fauzieuy

I'm using multischema app with postgres.
before try spreadsheet for my application, I tried to git clone app https://github.com/jalagrange/excel_test_app everything goes well.

https://github.com/jalagrange/excel_test_app/blob/master/app/controllers/people_controller.rb#L125

  def parse_save_from_excel
    test_file = params[:excel_file]
    file = FileUploader.new
    file.store!(test_file)
    book = Spreadsheet.open "#{file.store_path}"
    sheet1 = book.worksheet 0
    @people = []
    sheet1.each 1 do |row|
      p = Person.new
      p.first_name = row[0]
      p.last_name = row[1]
      p.age = row[2]
      @people << p
    end
    file.remove!
  end

And the problem appears when all of them are applied in my application.

def import
    @account = Account.find(current_adminsekolah.id)
    with_in_schemas :only => @account.schema do
    test_file = params[:excel_file]
    file = ExcelUploader.new
    file.store!(test_file)
    book = Spreadsheet.open "#{file.store_path}"
    sheet1 = book.worksheet 0
    @pelajar = []
    sheet1.each 1 do |row|
      p = Pelajar.new
      p.nama = row[0]
      p.nis = row[1]
      p.tglahir = row[2]
      p.jk = row[3]
      @pelajar << p
      p.save
     end
        redirect_to adminsekolah_pelajars_path
   end
end

admin and account under default public schema, but pelajar under account schema.
When i tried import, data is not imported.

Started POST "/admin/pelajars/import" for 127.0.0.1 at 2013-03-28 19:08:4
0 +0700
Processing by Admin::PelajarsController#import as HTML
  Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"bN8hAFJr9EF+/H1bX8U1qmVztf+
jfJdxBIJbtRq0b7Q=", "excel_file"=>#<ActionDispatch::Http::UploadedFile:0x541d238
 @original_filename="template_pelajar.xls", @content_type="application/vnd.ms-ex
cel", @headers="Content-Disposition: form-data; name=\"excel_file\"; filename=\"
template_pelajar.xls\"\r\nContent-Type: application/vnd.ms-excel\r\n", @tempfile
=#<File:C:/Users/zilgis/AppData/Local/Temp/RackMultipart20130328-2996-1tzwgsk>>,
 "commit"=>"Import"}
  Adminsekolah Load (2.0ms)  SELECT "public"."admins".* FROM "public"."ad
mins" WHERE "public"."admins"."id" = 3 LIMIT 1
  Account Load (2.0ms)  SELECT "public"."accounts".* FROM "public"."accounts" WH
ERE "public"."accounts"."id" = $1 LIMIT 1  [["id", 3]]
   (2.0ms)  BEGIN
   (1.0ms)  ROLLBACK
Redirected to http://lvh.me:3000/admin/pelajars
Completed 302 Found in 591ms (ActiveRecord: 47.0ms)
--- Select Schema: account
--- Restore Schema to public
@fauzieuy

Apparently the problem is not from the spreadsheet and multi-schema, I'm using devise so on controller such as

def import
    test_file = params[:excel_file]
    file = ImportUploader.new
    file.store!(test_file)
    book = Spreadsheet.open "#{file.store_path}"
    sheet1 = book.worksheet 0
    sheet1.each 1 do |row|
      email = row[0]
      temp_password = row[1]
      nama = row[2]
      nis = row[3]
      tglahir = row[4]
      jk = row[5]
      Pelajar.create(:email => email, :password => temp_password, :password_confirmation => temp_password, :nama => nama, :nis => nis, :tglahir => tglahir, :jk => jk)
    end
        redirect_to root_path
end
@fauzieuy fauzieuy closed this Mar 31, 2013
@zdavatz
Owner

Thanks for Sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment