# Exporting vcards from rails

**URL:** <https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102>\
**Category:** Rails\
**Created:** [August 10, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102 "2006-08-10T19:50:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 10, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102/1 "2006-08-10T19:50:40Z")

</div>

i’m doing a site for someone that has a form set up to collect contact  
information from the user. i installed the vpim gem and was hoping to  
give my client a way to download all of the contact’s information as a  
vcard. using the examples, i was able to get it working in ruby, but i’m  
not quite sure how to generate and download a vcard on the fly.

has anyone done anything like that?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 10, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102/2 "2006-08-10T22:10:36Z")

</div>

i was able to modify the examples to do what i needed. i would be  
interested in hearing from others to see if this is an ok way of  
generating the vcard when the user wants to download one:

require ‘vpim/vcard’

class VcardController \< ApplicationController

def get\_card  
contact = Contact.find(params[:id])  
card = Vpim::Vcard::Maker.make2 do |maker|

```
  maker.add_name do |name|
    name.given = contact.name
  end

  maker.add_addr do |addr|
    addr.location = 'home'
    addr.street = contact.address
    addr.locality = contact.city
    addr.region = contact.state
    addr.postalcode = contact.zip
  end

  if !contact.phone.empty?
    maker.add_tel(contact.phone) do |tel|
      tel.location = 'home'
      tel.preferred = true
    end
  end

  if !contact.fax.empty?
    maker.add_tel(contact.fax) do |tel|
      tel.location = 'home'
      tel.capability = 'fax'
    end
  end

  maker.add_email(contact.email) do |e|
    e.location = 'home'
  end
end

send_data card.to_s, :filename => "contact_#{contact.id}.vcf"

```

end  
end

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 10, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102/3 "2006-08-10T22:41:42Z")

</div>

Josh K. wrote:

> i was able to modify the examples to do what i needed. i would be  
> interested in hearing from others to see if this is an ok way of  
> generating the vcard when the user wants to download one:

> ```
> maker.add_name do |name|
> name.given = contact.name
> end
> 
> ```

I don’t think it’s possible to do this “right”, but you might want to  
try something like this, depending on the format of contact.name:

tmp = contact.name.split  
case tmp.size  
when 1  
n.given = tmp[0]  
when 2  
name.given = tmp[0]  
name.family = tmp[1]  
when 3  
name.given = tmp[0]  
name.family = tmp[2]  
when 4  
name.given = tmp[1]  
name.family = tmp[3]  
end

Cut to fit, paint to match:-)

–Al Evans

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [December 11, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102/4 "2008-12-11T12:32:27Z")

</div>

Derek Neighbors wrote:

> On Aug 10, 2006, at 10:50 AM, Josh K. wrote:
> 
> > i’m doing a site for someone that has a form set up to collect contact  
> > information from the user. i installed the vpim gem and was hoping to  
> > give my client a way to download all of the contact’s information as a  
> > vcard. using the examples, i was able to get it working in ruby,  
> > but i’m  
> > not quite sure how to generate and download a vcard on the fly.
> > 
> > has anyone done anything like that?
> 
> We are doing this using the SimplyRESTful stuff…
> 
> In the controller for example we have:
> 
> def show  
> respond\_to do |format|  
> format.html  
> format.xml { render :xml =\> @contact.to\_xml }  
> format.vcf do  
> send\_data @contact.to\_vcard, :filename =\> “#{@contact.id}.vcf”,  
> :type =\> ‘text/directory’  
> end  
> end  
> end

Using this technique gives me a server response with:

“uninitialized constant Mime::VCF”

I found several mime-type related files in the following directory:  
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/  
mime\_responds.rb  
mime\_type.rb  
mime\_types.rb

Is there a way to add the .vcf mime-type?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 15, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/exporting-vcards-from-rails/102102/5 "2006-08-15T14:15:34Z")

</div>

On Aug 10, 2006, at 10:50 AM, Josh K. wrote:

> i’m doing a site for someone that has a form set up to collect contact  
> information from the user. i installed the vpim gem and was hoping to  
> give my client a way to download all of the contact’s information as a  
> vcard. using the examples, i was able to get it working in ruby,  
> but i’m  
> not quite sure how to generate and download a vcard on the fly.
> 
> has anyone done anything like that?

We are doing this using the SimplyRESTful stuff…

In the controller for example we have:

def show  
respond\_to do |format|  
format.html  
format.xml { render :xml =\> @contact.to\_xml }  
format.vcf do  
send\_data @contact.to\_vcard, :filename =\> “#{@contact.id}.vcf”,  
:type =\> ‘text/directory’  
end  
end  
end

This allows us to get a contact back as html, xml or vcard.

Then in the contact model you can add something like:

def to\_vcard  
card = Vpim::Vcard::Maker.make2 do |maker|  
maker.add\_name do |name|  
name.prefix = prefix unless self.prefix.blank?  
name.given = first\_name  
name.family = last\_name  
name.suffix = suffix unless self.suffix.blank?  
end

```
   if preferred_location
     maker.add_addr do |addr|
       addr.preferred = true
       addr.location = 'work'
       addr.street = preferred_location.address_line_1 unless

```

preferred\_location.address\_line\_1.blank?  
addr.locality = preferred\_location.city unless  
preferred\_location.city.blank?  
addr.postalcode = preferred\_location.postal\_code unless  
preferred\_location.postal\_code.blank?  
addr.country = preferred\_location.country unless  
preferred\_location.country.blank?  
end

```
     maker.add_tel(preferred_location.voice_number) do |tel|
       tel.location = 'work'
       tel.preferred = true
     end unless preferred_location.voice_number.blank?

     maker.add_tel(preferred_location.fax_number) do |tel|
       tel.location = 'work'
       tel.capability = 'fax'
     end unless preferred_location.fax_number.blank?

     maker.add_email(preferred_location.email) do |e|
       e.location = 'work'
       e.preferred = 'yes'
     end unless preferred_location.email.blank?
   end

 end

 card.to_s

```

end

–  
Derek Neighbors  
Integrum Technologies

> **[Scrum and Agile coaching by Integrum](https://integrumtech.com/)**
>
> Integrum provides Scrum and Agile coaching that empowers team and business transformation.

Work: 602.792.1270 x1101  
Mobil: 480.330.7892  
Email: removed\_email\_address@domain.invalid
