What's New in Edge Rails: render Stops Being High-Maintenance

Posted by ryan
at 9:33 PM on Wednesday, November 19, 2008

This feature is scheduled for: Rails v2.3/3.0

render is one of those oft-used view-helper methods that always seems to be a little cumbersome. The most common use is to render a partial within another view:


render :partial => 'articles/article', :locals => { :article => @article }

But this is a lot of work for a simple operation, and now it becomes much simpler. Now the default is to assume that a partial is requested (in the past render a :file was the default) and that the final hash argument is the locals hash. Here is the above functionality using the new syntax:

1
2
3
4
5
6
7
8
9
# Render the 'article' partial with an article local variable
render 'articles/article', :article => @article

# Or even better (same as above)
render @article

# And for collections, same as:
# render :partial => 'articles/article', :collection => @articles
render @articles

If you’ve got some old render calls hanging around that aren’t using partials you’ll have to specify the :file option now:


render :file => 'original'

Hassle free partial rendering. Yay.

tags: ruby, rubyonrails

Comments

Leave a response

  1. Maximilian SchulzNovember 20, 2008 @ 07:38 AM

    Great news… I loved the better_partials plugin which at least provided the feature of allowing a locals hash as the last param.

  2. Jon MaddoxNovember 20, 2008 @ 10:27 AM

    Is this change on a new branch? Or is it in the soon to be final 2.2? I thought 2.2 was in RC2. This is a pretty big computability change to incorporate during the RC stage. Seems like the 2.2 branch should be in a bug squash mode with features frozen.

  3. Ryan BatesNovember 20, 2008 @ 11:15 AM

    This change is for 2.3/3.0 as mentioned in the changelog of the commit. BTW, it’s currently possible to do this in earlier versions:

    render :partial => @article

    and

    render :partial => @articles

    So from my understanding this change just takes out the need for the :partial option?

  4. Daniel SchierbeckNovember 20, 2008 @ 11:17 AM

    Jon: There’s a separate branch for the 2.2 series—this is happening on Edge (i.e. the master branch), and doesn’t impact 2.2.

  5. grosserFebruary 02, 2009 @ 02:40 AM

    hope it supports render ‘something’ do;end; too