I have managed to get the Flickr API to work using RoR with the Flickr gem. However, it seems to return a lot of info (as a string) related to the photos before showing the photos e.g.
[#<Flickr::Photo:xxxxxx @rotation="0", @client=#<Flickr:xxxx
@host="http://api.flickr.com", @activity_file="flickr_activity_cache.xml",
@api="/services/rest", @api_key="xxxxxxxxx">, @api_key="xxxxxx", @title="JAPAN",
@id="xxxx".....etc
The above is just a snippet of the info returned with some info replaced with xxxx. How do I remove this info?
The controller is as follows:
class FlickrController < ApplicationController
def flickrsearch
flickr = Flickr.new
if params[:text].nil?
#render :text => '<h2>Please enter a search string</h2>'
else
begin
@photos = flickr.photos(:text => params[:text], :per_page =>'10', :sort => 'relevance')
render "display"
end
end
end
and the view is as follows:
<div class="flickr">
<table>
<%= @photos.each do |p| %>
<td>
<%= image_tag(p.sizes[0]['source']) %><br>
<%= p.title %>
</td>
<% end %>
</table>
</div>
The 'photos' method within the class 'Flickr' in the Flickr gem is as follows:
def photos(*criteria)
photos = (criteria[0]) ? photos_search(criteria[0]) : photos_getRecent
# At this point, search criterias with pet_page => has structure
# {"photos => {"photo" => {...}}"}
# While per_page values with > 1 have
# {"photos => {"photo" => [{...}]}"}
collection = photos['photos']['photo']
collection = [collection] if collection.is_a? Hash
collection.collect { |photo| Photo.new(photo['id'], @api_key) }
end
I'm a bit of a newbie so apologies if this is a simple question. I have a feeling this is to do with me not pointing to the right place in the array??
Thanks.
As you already found out there is something wrong with your ERB syntax. In short, <% ruby.code %>
runs the ruby code and does not output anything, while <%= ruby.code %>
runs the ruby code and outputs the result of that into the HTML file (after doing some ruby result to string to HTML conversion.)
That means that the original code not only prints out the image tags, but also the result of @photos.each
, which is the same as @photos
, and then results in the output you have seen.
<%= @photos.each do |p| %>
<%= image_tag(p.sizes[0]['source']) %><br>
<% end %>
...while this one only outputs the image tags:
<% @photos.each do |p| %>
<%= image_tag(p.sizes[0]['source']) %><br>
<% end %>