screen-shot-2016-10-30-at-11-38-58-pm

[Ruby] AngularJSとCGI::Session

[Ruby] AngularJSとCGI::SessionAngularJSでセッション管理のおぼえがき

ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]

1. とりあえず、new_session => falseを試した

def create_session(cgi)
  session = CGI::Session.new(cgi,
                             'new_session' => true)
  session['account'] = cgi['account_id']
  session.close
end

def read_existing_session(cgi)
  begin
    session = CGI::Session.new(cgi,'new_session' => false)
    return session['account']
  rescue ArgumentError
    ApiRequest.logging('read_existing_session')
  end
  nil
end

2. ArgumentErrorエラーがでる

/ruby/2.0.0/cgi/session.rb:265:in `initialize': session_key `_session_id' should be supplied (ArgumentError) 

3. session_idオプションで解決

def create_session(cgi)
  session = CGI::Session.new(cgi,
                             'new_session' => true)
  session['account'] = cgi['account_id']
  session.close
  session.session_id
end

def read_existing_session(cgi)
  begin
    session = CGI::Session.new(cgi,
                               'new_session' => false,
                               'session_id' => cgi['session_id'])
    return session['account']
  rescue ArgumentError
    ApiRequest.logging('read_existing_session')
  end
  nil
end

参考サイトさま

About the author
  • コメントを残す