我正在从flickr中检索个人照片(专辑)的列表。如何从这个哈希数组中提取id,然后使用flickr.photosets.getPhotos列出每个相册中的图像?我还在学习Ruby,感觉非常困惑。我很感激任何朝着正确方向的努力。
当我尝试将set.each do .. anything
添加到@photosets
块时 - 我为'each'得到了一个no方法错误。
photos_controller.rb
require 'flickraw'
class PhotosController < ApplicationController
def index
FlickRaw.api_key = ENV['FlickRaw_api_key']
FlickRaw.shared_secret = ENV['FlickRaw_shared_secret']
@photosets = flickr.photosets.getList(user_id: '67921947@N02').each do |set|
set
end
end
end
照片/ index.html.erb
<h1>Listing photosets</h1>
<%= @photosets %>
当前输出
列出photosest
{
"id" =>"72157633435549025",
"primary" =>"8720558709",
"secret" =>"d6f79d3d3c",
"server" =>"7395",
"farm" =>8,
"photos" =>"8",
"videos" =>0,
"title" =>"Poetry Project",
"description" =>"",
"needs_interstitial" =>0,
"visibility_can_see_set" =>1,
"count_views" =>"4",
"count_comments" =>"0",
"can_comment" =>0,
"date_create" =>"1368044794",
"date_update" =>"1368044886"
},
{
"id" =>"72157633308505122",
"primary" =>"8674031898",
"secret" =>"8cd5722def",
"server" =>"8543",
"farm" =>9,
"photos" =>"22",
"videos" =>0,
"title" =>"Photo Midterm",
"description" =>"",
"needs_interstitial" =>0,
"visibility_can_see_set" =>1,
"count_views" =>"2",
"count_comments" =>"0",
"can_comment" =>0,
"date_create" =>"1366676884",
"date_update" =>"1366943480"
}
答案 0 :(得分:1)
你几乎就在那里。如果您打算在index.html.erb
上输出照片集及其相应照片的数据,您可以尝试以下内容:
@photosets = flickr.photosets.getList(user_id: '67921947@N02').each do |set|
set["photos"] = flickr.photosets.getPhotos(photoset_id: set["id"])
end
这将输出如下内容:
{
"id" =>"72157633435549025",
"primary" =>"8720558709",
"secret" =>"d6f79d3d3c",
"server" =>"7395",
"farm" =>8,
"photos" =>"8",
"videos" =>0,
"title" =>"Poetry Project",
"description" =>"",
"needs_interstitial" =>0,
"visibility_can_see_set" =>1,
"count_views" =>"4",
"count_comments" =>"0",
"can_comment" =>0,
"date_create" =>"1368044794",
"date_update" =>"1368044886"
"photos" => [{"id" => "2484",
"secret"=>"123456",
"server"=>"1",
"title"=>"my photo",
"isprimary"=>"0"},
{"id" => "2483",
"secret" => "123456",
"server" => "1",
"title" => "flickr rocks",
"isprimary" => "1"}]
},
{ # etc... }
我不确定sets.each do .. anything
无法理解您的意思,因为我在您的代码中看不到sets
。
答案 1 :(得分:0)
我想出来了 - 这是我用过的代码。这样您就可以获取有关个人相册(也称为“照片”的信息)的信息,然后提取每张照片集中的照片。
<强>控制器强> 要求&#39; flickraw&#39;
class PhotosController < ApplicationController
def index
FlickRaw.api_key = ENV['FlickRaw_api_key']
FlickRaw.shared_secret = ENV['FlickRaw_shared_secret']
@photosets = flickr.photosets.getList(user_id: '67921947@N02').map do |set|
flickr.photosets.getPhotos(photoset_id: set['id']).photo.map do |photo|
FlickRaw.url_b(photo)
end
end
end
end
查看强>
<% @photosets[0].each do |image| %>
<%= image_tag image %>
<% end %>