简体   繁体   English

如何从URL在RoR中填充数据库?

[英]How to populate database in RoR from a URL?

I want to store json data which is stored at some url. 我想存储存储在某些URL的json数据。 How can that be done? 那怎么办?

Like the comment above, I would recommend a gem like httparty . 像上面的评论一样,我会推荐像httparty这样的httparty You can send a get request to the desired url, like response = HTTParty.get('www.exampleurl.com') and use the rails json parsing gem/feature to parse it and create a record from that. 您可以将get请求发送到所需的url,例如response = HTTParty.get('www.exampleurl.com')并使用rails json解析gem / feature来解析它并从中创建记录。 But to sum things up: 但总结一下:

include 'httparty'

response =   HTTParty.get('www.exampleurl.com')
json_response = JSON.parse(response)

json_response.each do |json|
   Object.create(name: json.name, some_field: json.some_field)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM