Ruby on rails 在Rails模型中存储JSON数据
我是rails新手,对于如何从API调用获取JSON字符串并将其存储到模型中有点迷茫。我在兜圈子,想弄明白 例如,我从API调用中收到以下消息 {FileType=>nil,Text=>nil,TextCode=>nil,Html=>nil, HtmlCode=>nil,Rtf=>nil,RtfCode=>nil,CreditsRemaining=>0, FileExtension=>nil,Pdf=>nil,PdfCode=>nil, CandidateImage=>nil,CandidateImage扩展=>nil, 代码=>错误,子代码=>身份验证错误,消息=>无效 AccountId:您提供的“AccountId”无效。, ParsedDocument=>nil} 我想把它插入我的简历模型中,所以我创建了以下助手Ruby on rails 在Rails模型中存储JSON数据,ruby-on-rails,json,database,Ruby On Rails,Json,Database,我是rails新手,对于如何从API调用获取JSON字符串并将其存储到模型中有点迷茫。我在兜圈子,想弄明白 例如,我从API调用中收到以下消息 {FileType=>nil,Text=>nil,TextCode=>nil,Html=>nil, HtmlCode=>nil,Rtf=>nil,RtfCode=>nil,CreditsRemaining=>0, FileExtension=>nil,Pdf=>nil,PdfCode=>nil, CandidateImage=>nil,Candidate
我得到一个错误,脚本的语法不正确。请让我知道我在这里做错了什么,或者指出了正确的方向。这是我在将来需要将JSON返回保存到进程时所做的 1迁移 rails g迁移AddJsonResponseToModel
#/db/migrate/20170518014529_add_json_response_to_model.rb
class AddJesonResponseToModel < ActiveRecord::Migration[5.0]
def change
add_column :models, :json_response, :text
end
end
2控制器或您是否得到过响应
#app/controllers/models_controller.rb
class ModelsController < ApplicationController
def create
model = Model.find(params[:id])
response = RestClient::Request.execute(
method: :post,
url: 'http://example/api',
payload: params,
headers: {"Content-Type" => "application/json"}
)
model.json_response = response.to_json
...
model.save
end
end
我希望这对你有用:
快乐黑客1您应该发布错误消息,因为这通常会有所帮助,2这是您的实际代码吗?如果是这样的话,至少看起来您没有用一个端点关闭def。\ServiceKey\n这部分可能是错误的
#app/controllers/models_controller.rb
class ModelsController < ApplicationController
def create
model = Model.find(params[:id])
response = RestClient::Request.execute(
method: :post,
url: 'http://example/api',
payload: params,
headers: {"Content-Type" => "application/json"}
)
model.json_response = response.to_json
...
model.save
end
end