将 current_user 与 API 调用 Rails 的 json 响应相关联
Associate current_user with json response from API call in Rails
我需要将当前用户与我正在捕获并存储在 PostgreSql 9.4 中的 json 响应联系起来。我正在使用 Rails 4。我能够成功地将 json 存储在 json 数据类型的列 'return' 中。我已采取措施创建模型关联并更新模式,并且我有一个带有记录的用户模型,但 user_id 在包含 json return 的文档记录中仍然为零。我列出了控制器,因为我几乎确信我的问题就在这里。
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
我需要将当前用户与我正在捕获并存储在 PostgreSql 9.4 中的 json 响应联系起来。我正在使用 Rails 4。我能够成功地将 json 存储在 json 数据类型的列 'return' 中。我已采取措施创建模型关联并更新模式,并且我有一个带有记录的用户模型,但 user_id 在包含 json return 的文档记录中仍然为零。我列出了控制器,因为我几乎确信我的问题就在这里。
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