Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 + Ajax: how to access my local form builder instance

I have a form that displays a set of inputs. I also have a button, and when clicked, I make an ajax request which is supposed to replace the existing inputs with a different set of inputs.

All my ajaxy linking stuff up works fine. The problem is that I'm using a form_for, so in order to display the new form inputs, I need the form builder instance.

View File

<%= simple_form_for @order do |f| %>
    <div id="info">
        <%= render 'my_first_form_fields_partial', f: f %>
    </div>
    <%= link_to 'Change inputs', change_inputs_path, remote: true %>
<% end %>

I'd like my js.erb to look like this, but f is only defined within the scope of the form in the view.

$('#info').html("<%= escape_javascript(render 'my_second_fields_partial', f: f) %>");

How can I work it so that I can get f into that partial somehow?

like image 733
Derek Harrington Avatar asked Jun 08 '12 03:06

Derek Harrington


1 Answers

Can you use fields_for in your partial, and pass the @object to it? That way you don't need to pass a form builder?

partial:

<%= fields_for object do |f| %>
  f.text_field :field_name
<% end %>


$('#info').html("<%= escape_javascript(render 'my_second_fields_partial', object: @object) %>
like image 86
flynfish Avatar answered Nov 15 '22 17:11

flynfish