【发布时间】:2017-06-28 15:32:29
【问题描述】:
请原谅我的菜鸟问题。
我在我的 Rails 应用程序中使用 https://newsapi.org/,并且我正在使用 httparty gem。
def news
@techcrunch = HTTParty.get('https://newsapi.org/v1/articles?source=techcrunch&apiKey=766c2f65e93c477451455xxxxxxxxxxx',
:headers =>{'Content-Type' => 'application/json'} )
respond_to :html, :json
end
news.html.erb
<% if (@techcrunch.nil? or @techcrunch== []) %>
<p> <h2>No news found.. Sorry</h2></p>
<% else %>
<% @techcrunch.each do |techcrunch| %>
<td><%= link_to(image_tag(techcrunch["urlToImage"], height: '100', width: '100'), techcrunch.articles["url"])%></td>
<td><%= link_to(techcrunch["title"], techcrunch["url"]) %></td>
<td><%= techcrunch["publishedAt"] %></td>
<% end %>
<% end %>
我现在收到一个错误
没有将字符串隐式转换为整数
排队
<td><%= link_to(image_tag(techcrunch["urlToImage"], height: '100', width: '100'), techcrunch.articles["url"])%></td>
我在邮递员中检查了API,响应如下
{
"status": "ok",
"source": "techcrunch",
"sortBy": "top",
"articles": [
{
"author": "Matthew Panzarino",
"title": "Apple Music’s first new personalized playlist wants you to Chill",
"description": "This week Apple is beginning to roll out the first new personalized playlist under Apple Music’s ‘For You’ section. The playlist, entitled ‘Chill’..",
"url": "https://techcrunch.com/2017/06/27/apple-musics-first-new-personalized-playlist-wants-you-to-chill/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/06/img_0021.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-06-28T02:45:58Z"
},
{
"author": "Josh Constine",
"title": "Facebook now has 2 billion monthly users… and responsibility",
"description": "Thirteen years after launching and less than five years after hitting 1 billion, Facebook now has 2 billion monthly active users. If getting to 1 billion was..",
"url": "https://techcrunch.com/2017/06/27/facebook-2-billion-users/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/06/facebook-users-snapchat-twitter-youtube-whatsapp-instagram-wechat-qq.png?w=764&h=400&crop=1",
"publishedAt": "2017-06-27T17:06:05Z"
}
]
}
如何使用 news.html.erb 显示所有文章?
非常感谢任何帮助。在此先感谢!
【问题讨论】:
标签: ruby-on-rails ruby rest