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

On Fri, May 16, 2008 at 3:13 PM, Michael S. [email protected] wrote:

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

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

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 %>

<% 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.

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

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.

On Mon, May 19, 2008 at 8:45 PM, Rick DeNatale [email protected]
wrote:

defining it.

My blog on Ruby
http://talklikeaduck.denhaven2.com/


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

On Sun, May 18, 2008 at 10:05 PM, Ryan B. (Radar)
[email protected] 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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs