Sessions in Radiant Help

Hey all,
I know this discussion has taken place before but I still can’t
get my hands around it. I’m using radiant with the Rails_support
extension which works great with radiant for the content. The problem is
when I try to use the session inside that app in the extensions, The
session doesn’t hold any data. The session that comes with radiant only
get stuff written to it when I try to access the radiant admin app.

Does any one have a solution for that or even a way around it. I really
appreciate the help.

Thanks

Maged

Jeff Dean wrote:

I believe that this is because of a bug in rails, and not radiant.
The SiteController has session :off, and adding session :on in your
controllers doesn’t seem to do anything.

The only way I know to change this is to actually go into
SiteController and comment out that line. I’ve seen posts about
putting things like:

SiteController.class_eval{session :on}

in the activate method of your extension, but that didn’t work for me.

To get it to work, I did froze to edge radiant, then went into
SiteController.rb and commented out the session line. This would mean
that any pages that you want sessions off for you’d have to do
manually - so use with caution.

Jeff

Thanks for your help. Could you explain your last paragraph in more
details, I’m totally new to Radiant. where can I find the
SiteController.rb? any examples would be very helpful

My extension rails_support_extension.rb looks like this

**
require_dependency ‘application’

class RailsSupportExtension < Radiant::Extension
version “1.0”
description "Allows you to render Rails views in Radiant Layouts. "
url “Google Code Archive - Long-term storage for Google Code Project Hosting.

def activate
end

def deactivate
end

SiteController.send :include, SiteControllerExtension
SiteController.helper SiteHelperExtension

end
**

Thanks in advance
Maged M.

I believe that this is because of a bug in rails, and not radiant.
The SiteController has session :off, and adding session :on in your
controllers doesn’t seem to do anything.

The only way I know to change this is to actually go into
SiteController and comment out that line. I’ve seen posts about
putting things like:

SiteController.class_eval{session :on}

in the activate method of your extension, but that didn’t work for me.

To get it to work, I did froze to edge radiant, then went into
SiteController.rb and commented out the session line. This would mean
that any pages that you want sessions off for you’d have to do
manually - so use with caution.

Jeff

Here’s what I did:

gem install --include-dependencies radiant
radiant myapp
cd myapp
[change database.yml]
rake db:bootstrap
rake radiant:freeze:edge
rake radiant:update
rake db:migrate
[create extension…]

Now you’ll have a full copy of radiant’s source code in your vendor/
radiant directory. Now go to

vendor/radiant/app/controllers

You’ll see site_controller.rb (which I mistakenly referred to as
SiteController.rb below) - comment out line 2, and sessions will now
be enabled for the whole app. If you’ve previously started your web
server, you may have to restart for the changes to take effect.

There are much more elegant solutions, I’m sure, but this one worked
for me. If you have a problem with edge radiant this might not work.
If that happens, let me know.

I haven’t dealt with that before, but if I were to do it, I would:
Determine what I meant by expired (let’s say 3 days for example)
Write a rake task that deletes the session
Write a cron script to run it nightly after the database backup

My rake task would probably look like:

namespace :db
namespace :sessions
task :delete => :environment do

CGI::Session::ActiveRecordStore::Session.delete_all([‘created_at < ?’,
3.days.ago])
end
end
end

My cron script would look something like this:

mkdir -p /var/backup/mysql
date=date "+%d"
mysqldump -ubackup -pmypass myapp_production | gzip > /var/backup/
mysql/myapp-$date.sql.gz
cd /var/www/apps/myapp/current && rake db:sessions:delete

Alternately, you could have it run every time an admin logs in
(assuming that your admins don’t mind the slight performance hit)

Jeff

Jeff Dean wrote:

Here’s what I did:

gem install --include-dependencies radiant
radiant myapp
cd myapp
[change database.yml]
rake db:bootstrap
rake radiant:freeze:edge
rake radiant:update
rake db:migrate
[create extension…]

Now you’ll have a full copy of radiant’s source code in your vendor/
radiant directory. Now go to

vendor/radiant/app/controllers

You’ll see site_controller.rb (which I mistakenly referred to as
SiteController.rb below) - comment out line 2, and sessions will now
be enabled for the whole app. If you’ve previously started your web
server, you may have to restart for the changes to take effect.

There are much more elegant solutions, I’m sure, but this one worked
for me. If you have a problem with edge radiant this might not work.
If that happens, let me know.

Hey Jeff,
Thanks a lot man, I tried it and it works perfectly. Just one
more question. Do you know how to delete that session from the database
when the session expired. I read some posts and they talked cron jobs
and things like that but I needed to be handled in my application. Do
you have a quick solution?

I really appreciate your help man

Maged

You can enable sessions for a particular extension by doing something
like this:

class YourExtension < Radiant::Extension
def activate
YourController.class_eval { session :disabled => false }
end
end

This works on Radiant 6.0.3. I found this on the mailing list. I do not
think this is documented.

Regards,
Erik.

Jeff Dean wrote:

in the activate method of your extension, but that didn’t work for me.

To get it to work, I did froze to edge radiant, then went into
SiteController.rb and commented out the session line. This would mean
that any pages that you want sessions off for you’d have to do
manually - so use with caution.

Jeff


Erik van Oosten
http://2008.rubyenrails.nl/

But this shouldn’t work because the method session is undefined for
yourextension_extension.rb.

That’s the error I get when I try to do what you said:
in activate': undefined methodsession’ for SolrTags:Module
(NoMethodError)

But I’m using radiant 6.0.4 so could be this. How could I get the method
session for my _extension.rb file?

Marc