[英]Associate current_user with json response from API call in Rails
我需要將當前用戶與我在PostgreSql 9.4中捕獲並存儲的json響應綁定在一起。 我正在使用Rails4。我能夠成功地將json存儲在json數據類型的“返回”列中。 我已經采取措施來創建模型關聯並更新了架構,並且我有一個帶有記錄的用戶模型,但是user_id在保存json返回的Document記錄中仍然為nil。 我列出了控制器,因為我幾乎確信我的問題就在這里。
require 'alchemyAPI/alchemyapi'
require 'json'
class AlchemyController < ApplicationController
def index
@documents = Document.all
end
def create
alchemyapi = AlchemyAPI.new()
response = alchemyapi.combined('url', params[:q], {'extract'=>'page-image, title, author, concept' })
puts JSON.pretty_generate(response)
if
Document.create(result: response)
flash[:notice] = "Input was successfully analyzed and persisted."
redirect_to action: 'index'
else
flash[:notice] = "Input was successfully analyzed and persisted."
redirect_to action: 'index'
end
end
end
解決的方法是按照if塊的前兩行中的說明添加當前用戶。
def create
alchemyapi = AlchemyAPI.new()
response = alchemyapi.combined('url', params[:q], {'extract'=>'page-image, title, author, concept' })
puts JSON.pretty_generate(response)
if
*@user = current_user*
*@user.documents.create(result: response)*
flash[:notice] = "Input was successfully analyzed and persisted."
redirect_to action: 'index'
else
flash[:error] = "There was a problem analyzing your input."
redirect_to action: 'index'
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.