Good notes posted by hosiawak
RSS feed
Naming fragment cache
One of the common ways of using fragment caching is to cache content that’s shared across the site (eg. left navigation, menus, widgets etc.) that looks and works the same regardless of the name of the action or controller calling it. In such cases it’s very easy to just use named fragment caching eg.:
<% cache('left_nav') do -%> <%= display_left_nav -%> <% end -%>

stub_chain is very useful when testing controller code
or any other chained method call type that you’d like to stub, example:
in your controller:
def new @user = current_site.users.new end
in your spec:
it "#new should assign a @user" do u = mock("User") controller.stub_chain(:current_site, :users, :new).and_return(u) assigns[:user].should == u end
whereas before you had to stub each chained method call separately:
it "#new should assign a @user" do u = mock("User") users = mock("Users collection", :new => u) site = mock("Site", :users => users) controller.stub!(:current_site).and_return(site) assigns[:user].should == u end
Please note that stub_chain was added to RSpec in version 1.2.6

A catch-all format
If you’d like to specify a respond_to only for 1 or a few formats and render something else for all other formats, eg: (action.rss returns a feed but action.html or action.js should just render 404), use format.all:
respond_to do |format| format.rss { render_rss } format.all { render_404 } end
Rails will render an empty string for all formats that don’t specify a response explicitly.

Universal partial
polymorphic_url is very useful if you want to create an universal partial that works for more than 1 type of object passed to it.
For example in you sidebar you might have a _sidebar.html.erb partial that’s supposed to display links to “Edit” and “Delete” actions. You can write it in such a way that it can be reused for different types of objects (in the example below we pass either a Post or a Note).
your_template.html.erb
<%= render :partial => 'shared/sidebar', :locals => { :obj => Post.new -%>
other_template.html.erb
<%= render :partial => 'shared/sidebar', :locals => { :obj => Note.new -%>
_sidebar.html.erb
<%= link_to "Edit", polymorhpic_url(obj, :action => 'edit') -%> <%= link_to "Delete", polymorphic_url(obj), :method => :delete -%>

Useful in migrations
The most common usage pattern for this method is probably in a migration, when just after creating a table you want to populate it with some default values, eg:
class CreateJobLevels < ActiveRecord::Migration def self.up create_table :job_levels do |t| t.integer :id t.string :name t.timestamps end JobLevel.reset_column_information %w{assistant executive manager director}.each do |type| JobLevel.create(:name => type) end end def self.down drop_table :job_levels end end

Helper method taking a block
Following the similar egzample by autonomous, here’s a simpler version when you just need to write a flexible helper method that takes a block.
For example, suppose you have a method that renders a tree:
def render_tree(ary, &block) concat("<ul>", block.binding) for elem in ary concat("<li>", block.binding) yield elem concat("</li>", block.binding) end concat("</ul>", block.binding) end
You can use it in your view, eg:
<% render_tree(@objects) do |elem| -%> <%= elem.title -%> <%= link_to 'delete', elem -%> <% end -%>
that would return for egzample:
<ul> <li> Test title <a href="delete">/elems/1</a> </li> </ul>
Testing concat
To test such helper methods, use the following pattern (a utility method added to your Rspec/unit test suite:
def render_for(root, options = {}) _erbout = '' render_tree(root, options) do |node| _erbout.concat(node.title) end _erbout end
and test like this (RSpec example):
it "should return abc" do render_for(object).should == 'abc' end

Overriding the default div class="fieldWithErrors"
By default fields that are invalid are wrapped in:
<div class="fieldWithErrors"> <input type="text" name="blah"> </div>
To override and wrap in spans instead of divs place the following in your environment.rb:
ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }
or to not use wrapping at all:
ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "#{html_tag}" }

Overriding the default div class="fieldWithErrors"
By default fields that are invalid are wrapped in:
<div class="fieldWithErrors"> <input type="text" name="blah"> </div>
To override and wrap in spans instead of divs place the following in your environment.rb:
ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }
or to not use wrapping at all:
ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "#{html_tag}" }