轨道3 +阿贾克斯:如何访问我的本地表单生成器实例我的、生成器、表单、轨道

由网友(你可爱的小祖宗)分享简介:我有一个显示一组输入一种形式。我也有一个按钮,点击后,我做一个Ajax请求这是应该有一组不同的输入替换现有的投入。 I have a form that displays a set of inputs. I also have a button, and when clicked, I make an ajax r...

我有一个显示一组输入一种形式。我也有一个按钮,点击后,我做一个Ajax请求这是应该有一组不同的输入替换现有的投入。

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.

我所有的ajaxy连接东西了正常工作。问题是,我使用的form_for ,所以为了显示新的表单输入,我需要的表单生成器实例。

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.

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

我想我js.erb看起来是这样,但 F 仅在视图中窗体的范围内定义。

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) %>");

如何解决它,这样我就可以得到 F 成部分不知何故?

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

推荐答案

你能在局部使用fields_for和@object传递给它?这样,你并不需要传递一个表单生成器?

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?

部分:

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

$('#info').html("<%= escape_javascript(render 'my_second_fields_partial', object: @object) %>
阅读全文

相关推荐

最新文章