Skip to main content
Question

Dynamically generate preview links using the Ruby SDK

  • May 21, 2025
  • 3 replies
  • 1 view

Forum|alt.badge.img

I've looked through the SDK docs and searched on community.box.com without finding any details.

 

The Ruby code to generate a preview link looks like this:

link_to 'View File', client.preview_link(client.file_from_path(filePath)) + '?showDownload=true'

 

But these links are only good for a short period and there’s high processing time to generate each link, which makes the page load slowly.  Are there any examples or suggestions as to how I can dynamically generate these links when needed?

 

Thank you

3 replies

Forum|alt.badge.img

 You can call the embed_url method from our Ruby SDK to generate a file preview linkThis method takes a Box file id as the parameter and returns back a file preview link.

 

preview_link = embed_url(file)

 

You can preview the file by accessing the link directly or embedding the link into an iFrame.

 


Forum|alt.badge.img

Hello Murtza, thank you for that. The embed_url() method does seem to draw the page quicker than the two I was using. But my issue remains that with approximately 40 files per page, drawing preview and download links for each takes a bit of time.

 

Also the links expire after a short period. To dynamically generate the link my approach is to use a jQuery click event, passing the file ID to that function. Then have that function call a Ruby function that will return the result from the embed_url() method.

 

Here's the code I have so far:

#Java function
html += ''

#html body
html_list += 'Preview'

 

#Ruby function
def filePreview(fileID)
link = client.embed_url(fileID))
return link
end

I believe I'd need to use ajax to call the Ruby function from javascript. Any suggestions/examples or a better approach would be much appreciated.

 

Thank you
Jeff


Forum|alt.badge.img

 Your approach of generating the file preview links asynchronously makes sense. As you noted, you can use Ajax to communicate between the browser/client and the server. On the server, you can use embed_url method to request the file preview link from our API. Then you can pass the file preview link back to the browser.