Ruby 哈希和 Tumblr API

Ruby Hash and Tumblr API

controller page, Json output from api
我正在尝试使用 ruby 在我的视图页面上显示来自用户 tumblr 帐户的 posts。我以前从未对 api 做过任何事情。我正在尝试使用哈希表。我的控制器代码是这样的:

@Posts = client.posts"zombieprocess1.tumblr.com"

在我的视图页面上使用 html 我有

<%=Posts%>

反应是这样的

{"blog"=>{"title"=>"Untitled", "name"=>"zombieprocess1", "total_posts"=>1, "posts"=>1, "url"=>"URL", "updated"=>1478191052, "description"=>"", "is_nsfw"=>false, "ask"=>false, "ask_page_title"=>"Ask me anything", "ask_anon"=>false, "followed"=>false, "can_send_fan_mail"=>true, "is_blocked_from_primary"=>false, "share_likes"=>true, "likes"=>1, "twitter_enabled"=>false, "twitter_send"=>false, "facebook_opengraph_enabled"=>"N", "tweet"=>"N", "facebook"=>"N", "followers"=>0, "primary"=>true, "admin"=>true, "messages"=>0, "queue"=>0, "drafts"=>0, "type"=>"public", "reply_conditions"=>3, "subscribed"=>false, "can_subscribe"=>false}, "posts"=>[{"blog_name"=>"zombieprocess1", "id"=>152689921093, "post_url"=>"URL", "slug"=>"", "type"=>"photo", "date"=>"2016-11-03 16:37:32 GMT", "timestamp"=>1478191052, "state"=>"published", "format"=>"html", "reblog_key"=>"NCDqGTzW", "tags"=>[], "short_url"=>"URL", "summary"=>"", "recommended_source"=>nil, "recommended_color"=>nil, "followed"=>false, "liked"=>true, "note_count"=>1, "caption"=>"", "reblog"=>{"tree_html"=>"", "comment"=>""}, "trail"=>[], "image_permalink"=>"url", "photos"=>[{"caption"=>"", "alt_sizes"=>[{"url"=>"URL", "width"=>400, "height"=>544}, {"url"=>"URL", "width"=>250, "height"=>340}, {"url"=>"URL", "width"=>100, "height"=>136}, {"url"=>"URL", "width"=>75, "height"=>75}], "original_size"=>{"url"=>"URL", "width"=>400, "height"=>544}}], "can_like"=>false, "can_reblog"=>true, "can_send_in_message"=>true, "can_reply"=>true, "display_avatar"=>true}], "total_posts"=>1} 

我尝试了很多不同的格式,但似乎无法只得到 post url。我的想法是获取 post_url 并嵌入它们中的每一个,这样它就会像在我的网页上的 tumblr 中一样显示。谁能帮帮我?

试试这个:

@posts['posts'].first['post_url']

或者,如果您的回复包含多个 post,您可以 return 全部像这样:

(0...@posts['total_posts']).map { |i| @posts['posts'][i]['post_url'] }

编辑:第二行代码固定大小写。我假设您根据 Ruby 约定使用小写变量,'@posts'。如果不是,您应该更改为小写变量,因为这可能会造成混淆。