0

i have a controller (User controller), this is used to create update and delete a user from db. Within the UserObserver class after_save is been defined as a method in UserObserver after each save in db i have to call another application through http with user_details(in hash format)

class UserObserver < ActiveRecord::Observer
 def after_save user_details
  begin
    http Net::HTTP.new("localhost", "8000")
    request = Net::HTTP::Post.new("/verification")
    request.add_field('Content-Type', 'application/json')
    request.body = user_details.to_json   
    response = http.request(request)
    @http_error = false if response.code == 200
  rescue Exception => error
    @http_error = true
  end
 end
end

i need to access @http_error variable in user_controller to know status of http request been made so that furthur action can be taken according to it.

rack koh
  • 43
  • 2
  • As far as I know there's no way for an Observer to communicate with a Controller much the same as how Models have no idea what context they were created in. If you need to communicate, an observer might not be the best way to do this. – tadman May 04 '16 at 15:26
  • can u suggest me any ways to solve this problem – rack koh May 05 '16 at 06:08
  • i have tried to create a module(http_request_to_another_api) in lib file from where the http request is been made and i have included module in both controller and observer but i am unable to access http_error variable (present in http_request_to_another_api) from controller while from observer http_error variable (present in http_request_to_another_api) is accessible – rack koh May 05 '16 at 06:16

0 Answers0