Rails 2.2.xでsession_idをもとにセッションにデータを突っ込む

例えば、外部にある認証系がこちらのAPIを叩く場合など、セッションが継続してないからsessionからデータを入れられないんだけど、session_idはわかってるからどうにかしたいな、というときに。

今更ながらRails 2.2.2で試してみた。2.3.x以降ではCGIモジュールは破棄されてRackに移行したので、全く変わってる気がする。

class SomeController < ActionController::Base
  session :off
  def index
    session_id = (get session_id ...)
    
    # CgiRequestとRackRequestに対応
    options = request.class.get_const("DEFAULT_SESSION_OPTIONS").merge(:session_id => session_id)
    my_session = CGI::Session.new(request.cgi, options.stringify_keys)
    my_session[:foo] = :bar
    my_session.close
  end
end

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt="">