Rails AJAX: My partial needs a FormBuilder instance

Gareth picture Gareth · Dec 16, 2008 · Viewed 8.8k times · Source

So I've got a form in my Rails app which uses a custom FormBuilder to give me some custom field tags

<% form_for :staff_member, @staff_member, :builder => MyFormBuilder do |f| %>
[...]
    <%= render :partial => "staff_members/forms/personal_details", :locals => {:f => f, :skill_groups => @skill_groups, :staff_member => @staff_member} %>  
[...]
<% end %>

Now, this partial is in an area of the form which gets replaces by an AJAX callback. What I end up doing from the controller in response to the AJAX request is:

render :partial => "staff_members/forms/personal_details", :locals => {:skill_groups => @skill_groups, :staff_member => @staff_member}

However, if I do that then the form breaks, as the FormBuilder object I used in the form_for is no longer available. Is there any way for me to use my custom FormBuilder object inside a partial used for an AJAX callback?

Answer

Edd picture Edd · Jan 2, 2009

Use fields_for inside your partial. It performs a similar task but without wrapping the form tags. See the API docs.