mardi 6 novembre 2018

Exporting information to a vcf with Ruby On Rails

So im using vpim to export contact information and instead of just exporting the vcf, its redirecting to a new page and just showing the vcard in html

heres my controller class

    class VcardController < ApplicationController
  def view
    @user = Employee.find_by_id(params[:id])
    puts @user
    @card = Vpim::Vcard::Maker.make2 do |maker|
      maker.add_name do |name|
        name.prefix = ''
        name.given = @user.first_name
        name.family = @user.last_name
      end
      if(!@user.mobile_phone.blank?)
        maker.add_tel(@user.mobile_phone){ |e| e.location = 'cell'}
      end
      if(!@user.work_phone.blank?)
        maker.add_tel(@user.work_phone){ |e| e.location = 'work'}
      end
      if(!@user.home_phone.blank?)
        maker.add_tel(@user.home_phone){ |e| e.location = 'home'}
      end
      if(!@user.home_email.blank?)
        maker.add_email(@user.home_email) { |e| e.location = 'home' }
      end
      if(!@user.work_email.blank?)
        maker.add_email(@user.work_email) { |e| e.location = 'work' }
      end
    end
    send_data @card.to_s, :filename => @user.last_name+"_contacts.vcf"
  end
end

and this is how im calling controller

  <%= link_to "Export Contacts", vcard_view_path(id: @employee.id), class: "btn btn-default btn-clear btn-spacing", role: "button"  %>

I don't want the link to redirect you to a new page, just start the download. Ideas?




Aucun commentaire:

Enregistrer un commentaire