ruby-on-rails - 如何使用 Rails 将外部 JSON 数据保存到 DB(sqlite)
问题描述
我有一个添加 json url 表单的表单,如果用户添加 json url 并提交表单,我想将所有 json 数据保存到数据库。[形式] 1
使用 gem 从 url 获取 json 数据httparty
效果很好,现在将 json 数据存储到 db json_url
json data
{
"restaurant_name": "Restaurant 3",
"address": "xyz address",
"country": "United States",
"currency": "USD",
"client_key": "12345",
"client_name": "Client 3",
"client_email": "test3@mail.com",
"client_phone": "9876",
"product_tier": "tier1",
"brand_logo_large": {
"ID": 37,
"id": 37,
"title": "bait-al-bahar-logo-design",
"filename": "bait-al-bahar-logo-design.png",
"filesize": 105071,
"url": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
"link": "http://codekyt.in/froodle-wp/projects/res-1-client-1/bait-al-bahar-logo-design/",
"alt": "",
"author": "1",
"description": "",
"caption": "",
"name": "bait-al-bahar-logo-design",
"status": "inherit",
"uploaded_to": 35,
"date": "2019-01-04 11:11:48",
"modified": "2019-01-04 11:13:01",
"menu_order": 0,
"mime_type": "image/png",
"type": "image",
"subtype": "png",
"icon": "http://codekyt.in/froodle-wp/wp-includes/images/media/default.png",
"width": 600,
"height": 500,
"sizes": {
"thumbnail": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-150x150.png",
"thumbnail-width": 150,
"thumbnail-height": 150,
"medium": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-300x250.png",
"medium-width": 300,
"medium-height": 250,
"medium_large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
"medium_large-width": 600,
"medium_large-height": 500,
"large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
"large-width": 600,
"large-height": 500
}
}
}
我为用户创建了一个表单并添加了 json url,
<%= form_tag("/settings/json_url", method: "post") do %>
<%= label_tag :json_url %>
<%= text_field_tag :json_url %>
<%= submit_tag 'submit' %>
<% end %>
在数据库中,
t.string :key
t.string :value
t.string :json_url
在控制器中,
def json_url
# I am getting confused here
end
解决方案
推荐阅读
- javascript - 为什么在函数之外创建节点时只有最后一个附加有效?
- html - 为什么媒体打印中横向页面的内容隐藏
- python - django forloop 计数器作为变量
- python - 如何获取url列表并在scrapy python中用于Web数据提取
- javafx - JavaFX 绑定布局属性不适用于动态添加的节点
- postgresql - PGPOOL 从节点处于“等待”状态
- google-apps-script - 使用 getSheetByName() 和 ui.prompt() 中插入的 var
- c# - 代表两个参与者互相比赛的足球队的正则表达式
- ruby-on-rails - active admin 首字母大写
- c++ - 将数组转换为向量