Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';t将JSON数据映射到模型_Ruby On Rails_Json_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Can';t将JSON数据映射到模型

Ruby on rails Can';t将JSON数据映射到模型,ruby-on-rails,json,ruby-on-rails-4,Ruby On Rails,Json,Ruby On Rails 4,当前正在尝试调用API并将属性映射到我的模型。e、 g.在我的模型中,从JSON响应到图像字符串的image_src字符串。但现在它得到了错误“没有将字符串隐式转换为整数” Feed.rb require 'httparty' require 'json' class Feed < ActiveRecord::Base include HTTParty base_uri 'https://extraction.import.io/query/runtime' has_many

当前正在尝试调用API并将属性映射到我的模型。e、 g.在我的模型中,从JSON响应到图像字符串的image_src字符串。但现在它得到了错误“没有将字符串隐式转换为整数”

Feed.rb

require 'httparty'
require 'json'

class Feed < ActiveRecord::Base
  include HTTParty
  base_uri 'https://extraction.import.io/query/runtime'
  has_many :entries

  # GET /feeds
  # GET /feeds.json

  def fetch_data

    response = self.class.get("/2365205f-8502-439e-a6d2-73988cfa03f1?&url=http%3A%2F%2F%2F")
    @elements = response.parsed_response["extractorData"]

    @elements.map do |image_info|
      self.entries.create(image: image_info['url'])
    end

  end
end
class Entry < ActiveRecord::Base
  belongs_to :feed
end
任何帮助都会很好

response = self.class.get("/2365205f-8502-439e-a6d2-73988cfa03f1?&url=http%3A%2F%2F%2F")
puts response.parsed_response
显示身份验证失败:

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>openresty/1.9.7.3</center>
</body>
</html>


def fetch_data
  ...
  @elements = response.parsed_response["extractorData"]

  # To access the image src's:
  image_srcs = @elements['data'].first['group'].map{ | z | z['image'].first['src']}

  image_srcs.each do |src|
    self.entries.create(image: src)
  end
end

401需要授权
401需要授权

openresty/1.9.7.3 def fetch_数据 ... @elements=response.parsed_response[“extractorData”] #要访问图像src,请执行以下操作: image_srcs=@elements['data'].first['group'].map{z|z['image'].first['src']} 图像_srcs.each do | src| self.entries.create(图像:src) 结束 结束
是否尝试获取url或img src之类的内容?我想将每个图像->src映射到模型中的“图像”字段OK,请参阅更新的答案。由于我已从请求中删除了API密钥,身份验证失败;)。您能在原始模型中包含答案吗?我将把你的补充放在哪里?谢谢你的回答,很好,你删除了API密钥。关于“您能在原始模型中包含答案吗?”,它在原始模型中。我已经更新了答案。
response = self.class.get("/2365205f-8502-439e-a6d2-73988cfa03f1?&url=http%3A%2F%2F%2F")
puts response.parsed_response
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>openresty/1.9.7.3</center>
</body>
</html>


def fetch_data
  ...
  @elements = response.parsed_response["extractorData"]

  # To access the image src's:
  image_srcs = @elements['data'].first['group'].map{ | z | z['image'].first['src']}

  image_srcs.each do |src|
    self.entries.create(image: src)
  end
end