What's New in Edge Rails: Collection Partial Variable Naming

Posted by ryan
at 8:12 PM on Sunday, July 06, 2008

From the little-but-useful department comes a new addition to Rails that lets you explicitly name the local variable exposed to a partial template when using a collection partial. So, for instance, in this statement:


render :partial => 'employees', :collection => @workers, :as => :person

each element of the workers collection will be exposed as person within the employees template. No longer are you hostage to your template name.

tags: ruby, rubyonrails

Comments

Leave a response

  1. Fabio PapaJuly 06, 2008 @ 09:07 PM
    <quote>No longer are you hostage to the singular inflection of your collection name.</quote>

    Isn’t the default to provide a local variable with the same name as the partial? So that “render :partial => “employee”, :collection => @people” would result in a local variable called “employee”?

  2. RyanJuly 07, 2008 @ 07:51 AM

    Quite right, Fabio, I’ve updated my post accordingly. Thanks!

  3. Josh BassettJuly 07, 2008 @ 07:48 PM

    Great news from the little-but-useful department! This is something which has mildly annoyed me since day dot with Rails. Thanks for keeping your ear to the trunk.

  4. JillianJuly 09, 2008 @ 09:25 PM

    thats some really clever stuff to add I will hav eot give it a try sometime.