Notes posted to Ruby on Rails

RSS feed
June 22, 2008
This note might be spam Show
June 20, 2008
10 thanks

Set cache time-to-live

You can specify time-to-live for the cached item in seconds with :expires_in option.

class ListsController < ApplicationController
  caches_action :index, :expires_in => 1.hour
June 20, 2008
13 thanks

Nil V.S. Empty String HTML Options

There is a difference between an empty string and nil value for options hash.

Code Sample

content_tag( :div, 'Hello World!', :class=>'') # => "<div class="">Hello World!</div>"
content_tag( :div, 'Hello World!', :class=>nil) # => "<div>Hello World!</div>"
June 18, 2008
8 thanks

Using memcached as a session store

Because of Ruby’s CGI library limitations, store cannot have any configuration options. Basically this means that you cannot easily run memcached on a different port (or with any non-default settings for that matter).

You can bypass this limitation with this ugly hack (environment.rb):

cache_params = *([memcache_servers, memcache_options].flatten)
CACHE = MemCache.new(*cache_params)
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!({ 'cache' => CACHE })

In your initializer block, just configure session_store normally:

config.session_store = :mem_cache_store

I think this should be fixed to work like cache_store= does.