Cooliris (formally known as PicLens) is a great way of viewing images on the web, and it's really easy to make your Ruby on Rails app compatible. You just need to create an xml version of your page and Cooliris will read it like an rss feed.
First create your Cooliris xml template in /views/layouts/cooliris.rxml
xml.instruct!
xml.rss "version" => "2.0", "xmlns:media" => "http://search.yahoo.com/mrss" do
xml.channel do
xml.title @title
xml.description @description
xml.icon "/images/your_logo.png"
@images.each do |image|
xml.item do
xml.title image.reference_number
xml.link images_url(:action => :view, :id => image)
xml.guid images_url(:action => :view, :id => image)
xml.media:content, :url => image.authenticated_s3_url('public'), :type => "image/jpeg"
xml.media:thumbnail, :url => image.authenticated_s3_url('public'), :type => "image/jpeg"
end
end
end
end
Obviously tweak the details where necessary, xml.link needs to point to the html page and the xml.media:content needs to be the path to the image itself.
Then in your controller action you need to make sure you tell rails what to do when the xml version of the page is requested...
images = Image.all
respond_to do |format|
format.html do
@cooliris = true
@images = images.paginate :page => params[:page]
end
format.xml do
@title = "My Images"
@description = "Description of my images"
@images = images
render :template => "/layouts/cooliris", :layout => false
end
end
Note that the xml version loads all images whereas the html version paginates. This means Cooliris wont be limited to just showing the images on that corresponding html page.
In the head of your layout template you need to add an auto discovery link that points to your xml page.
<% if @cooliris %>
<link rel="alternate" href="<%= request.request_uri + ".xml" %>" type="application/rss xml" />
<% end %>
I used an instance variable that is set in the controller action to ensure the link is only displayed when the Cooliris xml page is available.
You are probably all done now, unless you aren't using restful routes. Basically you need to ensure you have a route that accepts the :format param so that Rails can serve the xml version of the page. Eg.
map.connect '/images/:action/:id.:format', :controller => "images"
That's it!
About Paul
Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.
Follow Paul on Twitter
Email: paulsturgess [at] gmail.com
Comments...
I am trying to embed the cooliris wall in my website and I am just a begginer and I am totally lost. I need something in actual code that i could copy. Is that possible anybody?
Jonathan at 21 May 09 at 10:59
Hi Paul,
Many thanks for your code. However, even after many days experimenting, I have not managed to get it functioning. Some parts are not quite clear. You write" You just need to create an xml version of your page" Do you mean an xml file that can intrepet the cooliris.rxml feed?
You advise "Obviously tweak the details where necessary, xml.link needs to point to the html page"
Which html page are you referring to? You state "In the head of your layout template(which file are you referring to?) you need to add an auto discovery link that points to your xml page(which file are you referring to?). I notice that you have no "action" named in the controller, that you use
"images = Image.all" instead of @images = Image.all, and that you use cooliris.rxml instead of cooliris.rss.builder.
Many thanks for your help.
Frank
Frank O'Connor at 03 Apr 10 at 08:17
Hi Frank,
The html and xml versions of the page use the same controller action.
The second block of code in my post shows how to use a respond_to block to allow the controller action to differ based on whether the xml or html version is requested.
When the xml version is requested I use the cooliris.rxml layout.
I set a local variable of images in the controller action so that I can use it differently depending on the version of the page requested. For the html version I paginate, for the xml version I don't.
Hope that clears things up for you.
Paul.
Paul at 03 Apr 10 at 08:42
Hi Paul,
Thanks for your rapid reply. I largely understand what you wrote.
I'm using rails 2.3.5 plus mysql on localhost setup, online for use of cooliris.
What is the name of the controller you use? (is images suitable?) And why do you not enclose the controller code in an action eg. def view......end?
you say "In the head of your layout template you need to add an auto discovery link that points to your xml page."
Is it, in the head of cooliris.rxml, the layout template, where this code must be inserted?
Other layout templates should no longer function as ":layout=> false" has been used
in the controller
Is crossdomain.xml required in the server route for your code to function?
Many thanks Frank
Frank O'Connor at 05 Apr 10 at 06:26
Hi Paul,
Thanks for your rapid reply. I largely understand what you wrote.
I'm using rails 2.3.5 plus mysql on localhost setup, online for use of cooliris.
What is the name of the controller you use? (is images suitable?) And why do you not enclose the controller code in an action eg. def view......end?
you say "In the head of your layout template you need to add an auto discovery link that points to your xml page."
Is it, in the head of cooliris.rxml, the layout template, where this code must be inserted?
Other layout templates should no longer function as ":layout=> false" has been used
in the controller
Is crossdomain.xml required in the server route for your code to function?
Many thanks Frank
Frank O'Connor at 05 Apr 10 at 08:32
Got something to say?