Best way (other than a session) to store objects in a Rails controller?

I have a rail controller

class Controllername < application
  def method1
    obj = API_CALL
    session =obj.access_token 
     redirect_to redirect_url    #calls the API authorization end point 
                            #and redirects to action method2  
  end
  def method2    
    obj.call_after_sometime
  end
end

I call some API in method1, getting the object and keeping the access token and secrets in the session. method1completes the action.

After some time I’m calling method2, now the session (access token, secrets) is saved correctly.

But now inside method2I need to call the API call_after_sometimeusing OBJECT obj. But now it is objunavailable because I did not store it in the session (we get an SSL error storing encrypted objects).

I want to know what is the best way to save objin method1so that it can be used later inmethod2

EDIT:

when I tried to execute a Rails.cache or session, I get an error

 TypeError - no _dump_data is defined for class OpenSSL::X509::Certificate

Google, , .

+4
2

, , ,

class Controllername < application
  def method1
    obj = API_CALL
    Rails.cache.write("some_api_namespace/#{current_user.id}", obj)
    session =obj.access_token 
  end
  def method2
    obj = Rails.cache.read("some_api_namespace/#{current_user.id}")
    obj.call_after_sometime
  end
end

, , fetch read, api,

def method2
  obj = Rails.cache.fetch("some_api_namespace/#{current_user.id}") do
    method_1
  end
  obj.call_after_sometime
end

,

+6

: obj .

class Controllername < application
  def method1
    obj = API_CALL
    session[:obj] = obj
  end
  def method2    
    if obj = session[:obj]
      obj.call_after_sometime
    end
  end
end
+1
source

All Articles