【问题标题】:Rails : get json value in hidden_field form (giphy api)Rails:以 hidden_​​field 形式获取 json 值(giphy api)
【发布时间】:2016-12-21 12:44:42
【问题描述】:

我正在使用 Giphy 搜索 API 让我的用户搜索 gif 并让他们添加 gif。

这是我的代码的简化部分,以使其正常工作:

_Form.html.erb

<%= form_for Task.new do |f| %>
<%= f.hidden_field :gif, id: "GifUrl", value: "" %>
<input type="text" name="search" placeholder="Search gif">
<!-- This part render the gif search result -->
<div id="score"></div>
<% end %>

我从 giphy 获取 gif 详细信息的脚本的一部分

  this.displayResult = function(url) {
    const target = document.getElementById('score');
    const img = document.createElement('img');
    img.src = url;

    target.appendChild(img);
  }

我想要做的是抓取返回的 gif 链接的 html src 值,并通过 hidden_​​field 在我的表单中传递它,但它返回 : [object Object] 作为值,而不是正确的 gif 链接。

 => #<Task id: 285, description: "", user_id: 37, gif: "[object Object]">

我应该使用像 nokogiri 这样的 html 解析器还是我做错了什么? 怎样才能做到这一点?

谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails json giphy-api


    【解决方案1】:

    看起来当使用 giphy-api 时你会得到一个 json response。您应该考虑只使用您想要存储的图像大小的 URL。浏览器

    $("#GifUrl").val(
      giphy_response['data'][SEARCH_RESULT_TO_USE][images][IMAGE_SIZE_TO_USE]['url']
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 2015-10-20
      • 2018-07-15
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多