Cleanest syntax for form partial?

When I'm in a form block and am including a partial, I use:

<%= render :partial => 'form', :locals => {:f => f} %>

Is there a cleaner way to write this? Something new in Rails 2 maybe?

Michael

I don't know of one off hand. What's dirty about that style?

Craig

Check this out:

http://www.railsjedi.com/posts/22-Better-Partials-Plugin-for-Ruby-on-Rails

Turns this:

<%= render :partial => 'form', :locals => {:f => f} %>

into:

<%= partial "form", :f => f %>

In Rails 2.1:

<% form_for ... do |f| %>    <%= render :partial => f %>

which renders _form.html.erb (*) where the local variable is called form.

*) or _foobar_form.html.erb if you use a custom FoobarFormBuilder

http://dev.rubyonrails.org/changeset/8646

Michael Slater wrote:

<% form_for … do |@f| %> <%= render :partial => “form” %> <% end %>

Even cleaner! Passing in your form object as an instance variable gives you the added bonus of having it available in any partial you render after defining it.

Cleanliness is in the eye of the beholder.

In any case, beware that Ruby 1.9 disallows using instance variables as block parameters, so if you're forward thinking this might be a technique to avoid.

Bugger!

I had a niggling thought at the back of my mind as I wrote that post that it was the case, but chose to ignore it.

Oh well, render :partial => “form”, :f => f looks like the cleanest way for now, unless you don’t mind not being Ruby 1.9 compatible, but it will come to bite you in the ass later on.