简体   繁体   English

在带有多层嵌套部分的Rails中使用表单构建器和DOM操作的问题

[英]Problem using form builder & DOM manipulation in Rails with multiple levels of nested partials

I'm having a problem using nested partials with dynamic form builder code (from the "complex form example" code on github ) in Rails. 我在Rails中使用带有动态表单构建器代码(来自github上“复杂表单示例”代码 )的嵌套部分代码时遇到问题。 I have my top level view "new" (where I attempt to generate the template): 我的顶层视图为“新”(尝试生成模板):

<% form_for (@transaction_group) do |txngroup_form| %>
<%= txngroup_form.error_messages %>
<% content_for :jstemplates do -%>
<%= "var transaction='#{generate_template(txngroup_form, :transactions)}'" %>
<% end -%>
<%= render :partial => 'transaction_group', :locals => { :f => txngroup_form, :txn_group => @transaction_group }%>

<% end -%>

This renders the transaction_group partial: 这使得transaction_group部分:

<div class="content">
<% logger.debug "in partial, class name = " + txn_group.class.name %>
<% f.fields_for txn_group.transactions do |txn_form| %>
<table id="transactions" class="form">
 <tr class="header"><td>Price</td><td>Quantity</td></tr>
 <%= render :partial => 'transaction', :locals => { :tf => txn_form } %>
</table>
<% end %>
<div>&nbsp;</div><div id="container">
<%= link_to 'Add a transaction', '#transaction', :class => "add_nested_item", :rel => "transactions" %>
</div>
<div>&nbsp;</div>

... which in turn renders the transaction partial: ...从而使交易成为部分交易:

<tr><td><%= tf.text_field :price, :size => 5 %></td>
<td><%= tf.text_field :quantity, :size => 2 %></td></tr>

The generate_template code looks like this: generate_template代码如下所示:

 def generate_html(form_builder, method, options = {})
      options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
      options[:partial] ||= method.to_s.singularize
      options[:form_builder_local] ||= :f  

      form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f|
        render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
      end
    end

    def generate_template(form_builder, method, options = {})
      escape_javascript generate_html(form_builder, method, options)
    end

(Obviously my code is not the most elegant - I was trying to get this nested partial thing worked out first.) (显然我的代码不是最优雅的-我试图首先解决此嵌套的部分问题。)

My problem is that I get an undefined variable exception from the transaction partial when loading the view: 我的问题是,加载视图时,我从事务部分获取了未定义的变量异常:

/Users/chris/dev/ss/app/views/transaction_groups/_transaction.html.erb:2:in _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in generate_html' /Users/chris/dev/ss/app/helpers/customers_helper.rb:28:in generate_html' /Users/chris/dev/ss/app/helpers/customers_helper.rb:34:in generate_template' /Users/chris/dev/ss/app/views/transaction_groups/new.html.erb:4:in _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/views/transaction_groups/new.html.erb:3:in _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/views/transaction_groups/new.html.erb:1:in _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/controllers/transaction_groups_controller.rb:17:in new' /Users/chris/dev/ss/app/views/transaction_groups/_transaction.html.erb:2 _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in '/Users/ _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in / _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in / _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in / _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in / _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in / _run_erb_app47views47transaction_groups47_transaction46html46erb_locals_f_object_transaction' /Users/chris/dev/ss/app/helpers/customers_helper.rb:29:in /chris/dev/ss/app/helpers/customers_helper.rb:28:在generate_html' /Users/chris/dev/ss/app/helpers/customers_helper.rb:34:in generate_template中/ Users / chris / dev / ss /app/views/transaction_groups/new.html.erb:4 _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/views/transaction_groups/new.html.erb:3:in _run_erb_app47views47transaction_groups47new46html46erb'/Users/chris/dev/ss/app/views/transaction_groups/ _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/views/transaction_groups/new.html.erb:3:in _run_erb_app47views47 / action46' /ss/app/views/transaction_groups/new.html.erb:1:在_run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/controllers/transaction_groups_controller.rb:17:in chris/ _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/controllers/transaction_groups_controller.rb:17:in ss/ _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/controllers/transaction_groups_controller.rb:17:in transaction_groups_controller.rb: _run_erb_app47views47transaction_groups47new46html46erb' /Users/chris/dev/ss/app/controllers/transaction_groups_controller.rb:17:in new'

I'm pretty sure this is because the do loop for form_for hasn't executed yet (?)... I'm not sure that my approach to this problem is the best, but I haven't been able to find a better solution for dynamically adding form partials to the DOM. 我很确定这是因为form_for的do循环尚未执行(?)...我不确定我对这个问题的解决方法是最好的,但是我还没有找到更好的方法用于将表单部分动态添加到DOM的解决方案。 Basically I need a way to add records to a has_many model dynamically on a nested form. 基本上,我需要一种在嵌套表单上动态将记录添加到has_many模型的方法。

Any recommendations on a way to fix this particular problem or (even better!) a cleaner solution are appreciated. 任何解决此特定问题或(甚至更好!)解决方案的建议都将受到赞赏。 Thanks in advance. 提前致谢。

Chris 克里斯

I suggest to use another example instead of this. 我建议使用另一个示例代替此示例。 I went also through this process recently and the best implementation is from ryan bates this: http://github.com/ryanb/complex-form-examples/tree/unobtrusive-jquery-deep 我最近也经历了这个过程,最好的实现是来自ryan bates: http : //github.com/ryanb/complex-form-examples/tree/unobtrusive-jquery-deep

If you are using prototype you that's not a problem, you can easily change the implementation to prototype from jquery. 如果您使用的是原型,那么这不是问题,您可以轻松地将实现从jquery更改为原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM