【问题标题】:How to cache/save image api results for each user and then cycle through it?如何为每个用户缓存/保存图像 api 结果然后循环浏览它?
【发布时间】:2016-09-13 18:24:12
【问题描述】:

我遇到了功能问题。这个想法是允许用户输入两个搜索词。让 Rails 查询像 google 或 bing 这样的图像 api,返回图像结果。然后循环显示结果,并排显示搜索的随机图像。

我的工作版本遵循这个逻辑:

Controller
  def new
    Array1 = ImageSearch("dogs")
    Array2 = ImageSearch("cats")

    @left_image = Array1.sample
    @right_image = Array2.sample
  end
end

我知道这很糟糕,因为每次访问 new 时都会对 api 进行新的调用。 如何使从 api 接收到的数据保持不变并且可以在接收后进行操作?

我最初的两个想法是将结果存储在数据库中(但这似乎效率低下),或者将 JSON 结果发送到浏览器并让它循环浏览图像。

【问题讨论】:

    标签: ruby-on-rails json api google-api bing-api


    【解决方案1】:

    我最终从 Bing 接收数据并使用 JSON 将其重新发送到浏览器。然后浏览器可以使用 jquery 循环浏览照片。

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多