I need to tie the current user with the json response that I am capturing and storing in PostgreSql 9.4. I am using Rails 4. I am able to successfully store the json in a column 'return' of json data type. I have taken measures to create the model associations and updated the schema, and I have a user model with a record but the user_id remains nil in the Document record holding the json return. I list the controller as I'm nearly convinced that my problem is here.
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
Solution was for me to add the current user as depicted in the first two lines of the if block.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.