A few tiny never/rarely seen before rails 2.0 features
Published over 5 years ago

I had talked about this before in the past. Basically, rails allows you to merge your css and javascript files in a single file which is cachable by the browsers. Hence, there’ll only be just one request ( which would return 304 response from 2nd time onwards ) per page, which would result in faster loading time for your visitors.
I tend to put these lines in my layouts :
<%= javascript_include_tag :all, :cache => true %> <%= stylesheet_link_tag :all, :cache => true %>
So let’s say to add a string called “name” to my users table, now I’d just do :
script/generate migration AddNameToUser name:string
And to remove a column called “useless” from users table :
script/generate migration RemoveUselessFromUser useless:string
Please note that, when you generate a migration to remove columns, attribute type may seem trivial. But it is used to generate down migration, which would re-create those columns.
In addition to what Ryan said, you can have inline exception handlers as well.
rescue_from ActiveRecord::RecordNotFound do render :file => '/bad_record', :status => 404 end
Block can even take have an argument for exception object.
rescue_from ActiveRecord::RecordInvalid do |exception| render :action => (exception.record.new_record? ? 'new' : 'edit') end
The above snippet would greatly DRY up your controllers if you use save! and create! in your RESTful application.
Rails now includes request profiler which uses ruby-prof and generates fancy pants graphs for you. You’ll need to supply an input file to use with request profiler, which would be in your usual integration test format. A sample input file for an application using restful_authentication plugin can look somewhat like :
lifo:freeonrails pratik$ cat perfscript post('/sessions', { :login => 'quentin', :password => 'test' })
And then roll it like :
lifo:freeonrails pratik$ script/performance/request perfscript
<%= render :partial => 'foo', :collection => [{ :name => 'world', :address => 'bar' }, { :name => 'food', :address => 'world' }] %>
render :partial => @pet.becomes(Cat) # renders cats/cat instead of pets/pet
<%= error_messages_for 'user', :object => @question.user %>
[].rand # => nil ['a'].rand # => 'a' [1,2,3].rand # => 1 or 2 or 3
{ :user => {:name => "lifo"} }.to_query => "user%5Bname%5D=lifo"
This post is for covering smaller fixes/features which are not very easy to spot. For very exciting and rather major Rails 2.0 features, I’d recommend you check out :