Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

Initialized! good, Authorized? Great!

2006-03-08   [ 0 comments ]

So the problem solving continues, each day a new challenge in this framework. However, someday I will make ruby.sing any tune I need and I'll be glad I fought it out.

Using the Model, View, Controller framework you are sometimes presented with a not so straightforward problem. My problem presented itself in the form of a stylesheet. That innocent .css file that makes everything on this page look the way it does. I had the style sheet, but I thought it would be nice to easily change the style sheet based on the method. That way if I decided method a should look different then method b (both in the same controller) then all I should have to do is change a variable. My first solution was quite simple:

I could add an instance variable to each method defining what stylesheet to use!

This approach is flawed, and without much more than an initial test I soon scrapped the idea. If I had went that route I would be forced into one particular problem:

Each method would have to have the variable. If I forgot one, I would be in trouble!

Thats when I decided to put the style sheet variable into some initial method that would load with each action. This would allow me to have a default for all the methods, and just override the default in any method I needed. So how do I do that? At first I wasnt sure... I tried just putting the @style_sheet variable outside of any method (of course that didn't work). After a few minutes I remembered something I had seen elsewhere. Define an initialize method and stick the instance variable in there. Just like:

def initialize
@style_sheet = "sapient.css"
end

(And a snippet from my 'user' layout)
<link href="/stylesheets/<%= @style_sheet %>" rel="stylesheet" type="text/css">

That done it! I now have all my methods using the same style sheet... and if I need to I can attach a different one just by changing the value of the instance variable in the method.

The next event of considerable mention (and in fact it took quite a bit of my time) is handling sessions. I have a basic CMS on this site, and I decided that it was a good time to add authentication to tighten up security. And what goes hand in hand with authentication on a web application? Sessions!

In the enjoyable read "Agile Web Development with Rails" I came across a good deal about sessions. The two options that I decided to choose between were- file held sessions, and database held sessions. The former stores session data in files in a temp folder, the latter stores the session data as rows in a table you setup for your application (conveniently named sessions). For scalability you would want to choose database session management (it is far easier to move a database to a seperate server than it is to use a network drive for session storage, in my opinion). So I decided - what the heck - why not go all out and do database sessions. Enter the SQL that created the sessions table:

CREATE TABLE `sessions` (
`id` int(11) NOT NULL auto_increment,
`sessid` varchar(255) NOT NULL default '',
`data` text NOT NULL,
`updated_at` datetime default NULL,
PRIMARY KEY (`id`),
KEY `session_index` (`sessid`)
);

Whichever method you chose be aware of one thing:

"People are often surprised that Ruby's session handler, which Rails uses, doesn't do automated housekeeping" - From the aforementioned book (pg 464)

Thats right! This obviously cannot be a feature of Ruby (if it is I would be shocked, but perhaps I am not seeing the complete picture). So be sure to implement some script or cron job to remove old session files or clear the old session records (otherwise trouble will eventually visit you).

To enable database session management I had to make a swift change in the config/environment.rb file in my rails project. Adding a line exactly like the following tells active record to store sessions for you in the database:

ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] = CGI::Session::ActiveRecordStore

That prepares you for session storing, next thing to do is write some authentication methods and test them out. This however will have to wait until part 2 of this post, look for it to be available tomorrow. I'll discuss a basic authentication setup (with code snippets for it to work) as well as anything else I run across between now and then.

:author => "Charles Abbott"
Converting to Ruby on Rails
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"