Search code examples
ruby-on-railsrubycookiessession-cookiesruby-on-rails-2

setting cookies directly in Rails2


Motivation: I'm solving problems that are caused by strange/mutated/odd/perverse cookies in ruby-on-rails. I have captured some of these evil cookies and I can use cookie manager on firefox to recreate the cookie state to replicate the problems. I want to write a rails page that will automate the task of setting these cookies.

Problem: If I change or delete the _myapp_session cookie in a rails controller, it issues me a new one. For example the following change to _myapp_session will never be seen by the user.

cookies[:_myapp_session] = 'abcedf'

I want to recreate the exact cookies for my domain that a user has including invalid or corrupted cookies. How can I get this low level with rails2?


Solution

  • It shouldn't create a new one. Are the cookies of a different domain than the source domain of the page? If so, you can set the domain on the cookies by passing a block to the cookie -setter method.

    cookies[cookie_name] = {
        :value => 'your value',
        :expires => 1.week.from_now,
        :domain => 'sub.yourdomain.com'
    }