1. Skip to navigation
  2. Skip to content

The ELC Community Blog

A knowledge exchange on Ruby on Rails and Agile Development


Patching Rails - Rendering form partials

by damian on January 16, 2008

Yesterday we got a patch committed to Rails. This new enhancement provides a nicer and more conventional way of rendering form fields as a partial.

Instead of writing this not-so-DRY call to

   1  render
,

  <% form_for(@client) do |f| %>
    <%= render :partial => 'form', :locals => {:f => f} %>
    <%= submit_tag 'Create' %>
  <% end %>

... we can do something cleaner, like this:

  <% form_for(@client) do |f| %>
    <%= render :partial => f %>
    <%= submit_tag 'Create' %>
  <% end %>

Here, the rendered partial is

   1  clients/_form
and the local variable inside the partial is called
   1  form
.

If we used a different form builder,

  <% form_for(@client, :builder => LabellingFormBuilder) do |f| %>
    <%= render :partial => f %>
    <%= submit_tag 'Create' %>
  <% end %>

... the partial that gets rendered is

   1  clients/_labelling_form
and the local variable is called
   1  labelling_form
.

There have been other proposals, but in the end we agreed that the

   1  render
method is the one who knows about the different kinds of things that can be rendered as partials (take, for example,
   1  render :partial => @clients
).

The patch also changes the docs and provides unit tests for the new behavior.

Comments

Add a comment


home | services | Ruby on Rails Development | code | blog | company