I'm creating a website in Ruby on Rails, where users can login using RESTful Authentication. Someone can get a specific user using html, xml and json, just like scaffolding. But I want to add one more format: vCard (e.g. /users/1.vcard). This has a specific format, but how do I define my own formats? Using views, or must I use another way? Thanks
+3
A:
In your /config/initializers/mime_types.rb file, add a new registration for your format. It should look something like this:
Mime::Type.register "text/x-vcard", :vcard #The :vcard is the important part
After that (you'll have to restart your app to pick up the change), you can respond to the symbol like any other format:
# then in your controller action
def show
respond_to do |format|
format.html # render html
format.vcard { #render vcard }
end
end
Adding from comments (thanks nanda):
In your views folder, then, you would put the vCard template into a show.vcard.erb file (for example).
jerhinesmith
2010-03-13 16:10:31
Thanks, but how do I actually render it? Using a view?
Time Machine
2010-03-13 21:20:39
Yes, using a view, something like show.vcard.erb
nanda
2010-03-13 22:13:03
@nanda thanks ^^
Time Machine
2010-03-14 13:20:53