15

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?

Gareth
  • 133,157
  • 36
  • 148
  • 157

5 Answers5

15

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

Edd
  • 166
  • 4
  • Hi, this is what I ended up doing in the end :) It's a bit awkward but it actually feels nicer passing objects around than form builders – Gareth Jan 04 '09 at 00:27
  • 30
    But what do you do in the situation where you actually need that form builder in there? e.g. when you're calling on nested fields (f.fields_for)? – Andrew Vit Jun 17 '09 at 22:01
  • 5
    Was there ever a solution to this? @AndrewVit I am still stuck on this matter 5 years after your comment. – Donato Oct 07 '14 at 22:34
  • 1
    See http://stackoverflow.com/questions/3881580/passing-a-form-object-to-a-partial-rendered-from-an-ajax-request for an explanation of how `fields_for` can be used the way @AndrewVit is asking. The TL;DR is you don't keep `f` as a receiver since it can't be in scope. Instead you assign your fields name statically (`fields_for "event[client_attributes]"`) and re-populate what you _will_ have in scope on the server (a model instance). – Alex Moore-Niemi Apr 12 '17 at 17:10
2

how about this?

  @template.with_output_buffer do
    @template.form_for @model_object do |f|
      f.fields_for :some_nested_attributes do |ff|
        render :partial => 'nested_attributes', :object => @model_object, :locals => {:form => ff}
      end
    end
  end

this would be especially useful is you need to use the nested fields_for in the partial

David Lowenfels
  • 1,000
  • 6
  • 5
  • Sorry about this old post, but what about the js.erb file? is not there where the problems is? Does your code refer to html.erb or js.erb file? – Albert Català Mar 10 '14 at 12:58
1

You can submit within your ajax call the content of f.object_name (it's also works with partials) and use it to render tags defined in http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html passing it as the first argument.

Kadu Diógenes
  • 508
  • 1
  • 6
  • 19
1

Maybe I'm a little late in the game here, and maybe I don't understand the question properly, but in ApplicationHelper.rb I think you can just add the line:

ActionView::Base.default_form_builder = MyFormBuilder
Samo
  • 8,202
  • 13
  • 58
  • 95
1

You could instantiate a new instance of your form builder in the controller, though it feels sort of lousy to me:

# in the controller
render :partial => {
  :f => MyFormBuilder.new(:staff_member, @staff_member, template),
  :skill_groups => @skill_groups,
  :staff_member => @staff_member
}

Alternatively, you could move more of the update logic to be client side which wouldn't require you to worry about rendering anything at all. You could just update the values via JS. Not sure if that works for your project though.

nakajima
  • 1,862
  • 12
  • 12