繁体   English   中英

将current_user与来自Rails中API调用的json响应相关联

[英]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.

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