Another look at integrating Scribd with your Rails application

Almost a year ago I wrote about integrating Scribd into your Rails application, and since then that feature has been working well in my applicant tracking system. Today, though, I got a request for information on how I display the documents that I send to Scribd, so I thought I’d share that, too.

In the previous post I showed how to send the document to Scribd using the API, and in this post I’ll show how I take the results from the API and use them in my application. First, here’s a snippet from my Attachment model that shows I create Scribd URLs based on the info that comes back from the API. The scribd_access_key and scribd_doc_id are database-backed methods whose values get populated with the response from the API. The scribd_url method is defined like so, simply creating a link to Scribd using the document ID and access key:

  def scribd_url

Here’s a snippet from my view that shows the attachments related to a job candidate’s submission. The preview link is conditional on the Scribd document ID being set, since not all documents that could be attached (e.g., resumes in WordPerfect format) will be successfully converted by Scribd. The user still gets a download link though.

< % if attachments.any? %> <ul class="attachments"> < % attachments.each do |att| <span>> <li id="attachment_<</span>= %>"> < %= link_to att.filename, attachment_url(att), :title => 'Download' %> < %= link_to(image_tag('icons/page_white_magnify.png'), att.scribd_url, :id => att.scribd_doc_id, :rel => att.scribd_access_key, :class => 'scribd', :title => 'Preview') unless att.scribd_doc_id.blank? %> </li> < % end %> </ul>

<div id="scribd" style="display:none; margin-top: 1em"> <a onclick="$('scribd').hide()">Close preview</a> <div id="scribd_preview" style="margin-top: 2px"> </div> </div> < % content_for :head do %> <script type="text/javascript" src=''></script> < %= javascript_include_tag 'scribd.js' %> < % end %> < % end %>

And here’s my javascript that I inject into the head of the document via that call to content_for. It takes the info from the preview link and passes that along to the javascript provided by Scribd for doing in-page embedding.

var scribd_doc;

function showScribd(link) { scribd_doc = new scribd.Document(, link.rel); scribd_doc.addParam('height', 400); scribd_doc.addParam('width', 620); scribd_doc.addParam('page', 1); scribd_doc.write('scribd_preview'); $('scribd').show();

Event.observe(window, 'load', function() { $$('a.scribd').each(function(link) { link.observe('click', function(event) { showScribd(link); Event.stop(event); }); });

So there you go, a complete Scribd integration for Rails, both server-side and client-side. Now it should be as easy as pie for you to bake Scribd into your own application.

Leave a Reply

Your email address will not be published. Required fields are marked *