Rails 3 + Ajax: how to access my local form builde

2019-03-13 05:15发布

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?

1条回答
聊天终结者
2楼-- · 2019-03-13 05:51

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) %>
查看更多
登录 后发表回答