我从get_connection
获得了koala gem的结果哈希值,看起来像这样
=> [{"album"=>{"created_time"=>"2011-05-07T23:06:33+0000", "name"=>"Timeline Photos",
"id"=>"10150170707957371"}, "images"=>[{"height"=>1150, "source"=>"https://scontent.xx.fbcdn.net/v/t31.0-8/24173690_10155086191327371_1463889654041392146_o.jpg?oh=8adec503c6066dc20d1be5d71262a03e&oe=5AFEED4A",
"width"=>2048}, {"album"=>{"created_time"=>"2011-05-07T23:06:33+0000", "name"=>"Timeline Photos",
"id"=>"10150170707957371"}, "images"=>[{"height"=>1188, "source"=>"https://scontent.xx.fbcdn.net/v/t31.0-8/24302317_10155086179077371_4000719398736973936_o.jpg?oh=6eba399a4067b847cb38ef245e687321&oe=5AFC195A",
"width"=>2639}]}]
我当时希望像使用params hash一样保存像rails这样的照片,但实现它只会为每张照片创建一个带有do
块的图片。
def photos(user)
photos = user.facebook.get_connection("me", "photos?fields=album,images,event,height,width,link,place&type=uploaded")
photos.each do |photo|
params = { gallery: {
name: 'Facebook Pictures', pictures_attributes: [
{ fb_album_created_time: photo['album']['created_time'],
fb_album_name: photo['album']['name'],
fb_album_id: photo['album']['id'],
fb_source: photo['images'][0]['source'],
fb_height: photo['images'][0]['height'],
fb_width: photo['images'][0]['width'],
fb_link: photo['link'],
fb_pic_id: photo['id'] }
]
}
}
gallery = user.galleries.new(params[:gallery])
gallery.save!
end
end
如何执行嵌套图片属性,以便将它们保存到一个图库中。即使是一颗可以帮助我的宝石。我正在看rails / jbuider gem,但不确定它是否更容易没有宝石?
答案 0 :(得分:0)
使用Jbuilder解决了这个问题。
def to_jbuilder
Jbuilder.new
end
def photos(u)
json = to_jbuilder
first_page_photos = u.facebook.get_connection("me", "photos?fields=album,images,event,height,width,link,place&type=uploaded")
photos = json.photo first_page_photos do |photo|
json.fb_album_created_time photo['album']['created_time']
json.fb_album_name photo['album']['name']
json.fb_album_id photo['album']['id']
json.fb_source photo['images'][0]['source']
json.fb_height photo['images'][0]['height']
json.fb_width photo['images'][0]['width']
json.fb_link photo['link']
json.fb_pic_id photo['id']
end
params = { gallery: { name: 'Facebook Pictures', pictures_attributes: photos }}
gallery = u.galleries.new(params[:gallery])
gallery.save!
end