Thursday, May 29, 2008

Disable rails session for robot (search engine crawler)

class ApplicationController < ActionController::Base
session :off, :if => lambda {|req| req.user_agent = ~/(Google|Slurp)/i}
# Google for Google and Slurp for Yahoo!.
# Explore your log for more robots

* Cited from The Rails Way

Rails RJS examples and official doc

With rjs, you can call the ajax helper without specifying the html elements. Moreover, you can do whatever you want after the action(update several divs, etc.).

render :update do |page|
page.replace_html 'div1', "HAHA"
page.replace_html 'div2', :partial => 'person', :object => @person

OR, you can create a rjs template:show.js.rjs, and put the rjs code there.

In the controller, you'll then have:
def show
...
#no render at all
end

Rails will pick the right template automatically.

Official rjs docs

Handy behavior driven Rails Ajax helpers

Periodically_call_remote:

periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block')

observe_field:
<%= observe_field :suggest, :url => { :action => :find_suggestion },
:frequency => 0.25,
:update => :suggest,
:with => 'q'
%>

Rails named route

map.purchase 'products/:id/purchase', controller => 'catalog'

can be invoked with purchase_url(:id => 1)

Rails 2.0 Root Route

The proper way of setting root route in Rails 2.0

map.root : controller => "homepage"