Rails Sessions

Posted by John
on Sunday, 09 March 2008

rails is love

To make a truly intelligent web application your gonna sooner or later have to play around with Sessions, which is basically a Cookie's big brother.

Store in DB

Now Rails initally will store all it's session data in a text file within the /tmp/sessions directory of your rails app, which is usually ok, but if you're gonna build a production-ready app you're gonna want to up the ante somewhat and store them in your DB.

This is simply done by opening up your environment.rb file and un-commenting the following line...

(within /config/environment.rb) config.actioncontroller.sessionstore = :activerecordstore

Then in the command-line generate a RAKE DB migration for your Session objects (so they're stored in your db from now on).

rake db:sessions:create rake db:migrate

Bingo, you now have a table within your database hooked up and ready to store your session data perfectly.

Storing Session Objects

Now that you've got your DB storing your session data, why don't we start creating some session objects.

This is done by...

session[:order] = 'ASC'

Here, we've simply created a new session object, storing in it a text value of 'ASC'; cool eh?

We can then check to see if our session is empty via...

if session[:order].blank? ..do stuff..

So if our session object contains nothing we can initialise it correctly.

Common Gotcha

Now one of the good things with switching sessions over to your db is that it'll secure your app a little bit better and your performance will improve.

Plus if your building your app on your dev machine, then transfer it to your live box, but can't find a reason why your sessions don't work there it's usually down to file permissions for the session file; by switching sessions over to a db you remove any future problem of this.

Good eh,

Injecting Elements After Load

Posted by John
on Tuesday, 12 February 2008

Here's a javascript technique which you might need down the line and which helped me out a lot when handling a ppc event. Basically the code below allows you to execute a script that would normally be run on load, but here we run it when we want to; because say in this case you don't want to reload the page just for an extra script.

Injecting Scripts After Load

So,

var oScript = document.createElement("script");
oScript.src = "signed_up_user.js";
document.body.appendChild(oScript);

What this does is create a new script element and append it to the current page.

The createElement() bit defines what kind of object you're going to create,

  • img => image
  • a => hyperlink
  • script => javascript

...giving you all the properties related to that object, so to set the script for a script object we do...

oScript.src = "signed_up_user.js";

The appendChild injects the element into the area you specify within the DOM model, in this case the body.

!! Document.Write !!

Important note, you can't fire a Document.Write after the page has loaded, just won't work.

To add elements to your page use document.body.appendChild or Prototype / MooTools, which add some really efficient handlers for you to do this even faster and cross-browser.

Injecting Images After Load

We can also add an image generated off of some ppc script we've been given (big help if you've got googleppc to handle), like...

var oScript = document.createElement("img");
oScript.height = "1";
oScript.width = "1";
oScript.src = "some_ppc_script_function()";
document.body.appendChild(oScript);

In this case our image element is generated with the appropriate width & height, and then the source is put in from our ppc function that returns back an image element and injects it into the page.

All without refreshing the page, but more importantly when you add that script to the page after-load it IS processed!.

Hardcore AJAX ;-)

Have fun!

Rake Migrations and Custom Tasks

Posted by John
on Sunday, 16 December 2007

Jesse James

One cool thing about Rails is it provides you with so many helper functions and commands, one of these being RAKE.

RAKE is similar to the MAKE compiler command used to build binaries from source, with Rails it's useful when building your apps database.

Database Work

Create Migration

script/generate migration users

This will create a Rails database migration you can use to specify the contents of the users table for instance.

/myapp/db/migrate/001_create_users.rb

Create Database

Once that's defined you can create the database with,

rake db:create

Compile Pending Migrations

Then run any pending database migrations.

rake db:migrate

All of which will build into a schema located at,

/myapp/db/schema.rb

Custom Tasks

You can also write custom RAKE tasks to do other things,

What to put in them

Here's a simple rake task,

task :sayHello do
  puts "Hello World"
end

Where to save your Scripts

Simple, in the tasks directory,

/myapp/lib/tasks

e.g.

/myapp/lib/tasks/sayhello.rake

And to Run ?

rake sayHello

Returns...

Hello World

More Info at...

CRUD and CRON Jobs

Posted by John
on Saturday, 04 August 2007

Create, Read, Update, Delete = CRUD

The four basic functions of any persistent storage mechanism.

Refers to all of the major functions that need to be implemented in a relational database application to consider it complete. Each letter in the acronym can be mapped to a standard SQL statement:

  • Create => INSERT
  • Read (Retrieve) => SELECT
  • Update => UPDATE
  • Delete (Destroy) => DELETE

It's really that simple and not only applies to databases but nearly any form of application.

CRONTAB and Cron Jobs

In simplest terms, a time-based scheduling service in Unix & Linux systems.

Driven by a configuration file named crontab that specifies shell commands to run periodically on a given schedule.

E.g.

  • Load the LighTPD web server when the server reboots