【问题标题】:TypeError - no implicit conversion of String into IntegerTypeError - 没有将字符串隐式转换为整数
【发布时间】: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


    【解决方案1】:

    问题出在这里:

    techcrunch.articles["url"]
    

    techcrunch["articles"] 是一个数组。您可能想要遍历文章:

    <% @techcrunch["articles"].each do |techcrunch| %>
    

    并删除.articles:

    techcrunch["url"]
    

    从哪里失败。

    【讨论】:

    • ,感谢您的快速回复。我尝试替换 techcrunch.articles[0]["url"]。现在我收到类似“undefined method `articles' for ["status", "的错误好的"]:数组"
    • 哦,抱歉,请查看更新:您应该首先迭代文章。
    • ,非常感谢。你的解决方案解决了我的问题。
    【解决方案2】:

    您应该检查控制台上的 api 响应,看看您得到了什么并从那里开始。

    def new
      ...etc
      puts @techcrunch
    end
    

    甚至在视图内部,在一切之前&lt;%= @techcrunch %&gt; 您可能正在循环一个字符串而不是一个对象,并且错误来自它是一个字符串,当您调用 ["urlImage"] 时,它应该是一个指示索引而不是字符串 ("urlImage") 的整数。在迭代之前,您需要先解析和组织响应。

    另外,你可以直接调用:

    if @techcrunch.blank?
    

    而不是 .nil? && == []

    【讨论】:

    • 如何回答上述问题?
    • 它告诉你怎么做。 “您需要先解析和组织响应,然后再遍历它。”,解释了错误并为所使用的语法提供了建议。您可以继续通过查询 API、找出响应并在此处编写来完成它,而不是浪费时间对一个模糊问题的完美答案进行评论。
    最近更新 更多