使用表单生成器和问题的问题Rails中的DOM操作具有多级嵌套部分

时间:2010-06-16 03:42:31

标签: javascript ruby-on-rails dom

我在使用Rails中的动态表单构建器代码(来自"complex form example" code on github)的嵌套部分时遇到问题。我的顶级视图是“new”(我尝试生成模板的地方):

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

这会使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>

...反过来将事务呈现为部分:

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

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

(显然我的代码不是最优雅的 - 我试图让这个嵌套的部分内容首先解决。)

我的问题是,在加载视图时,我从事务部分得到一个未定义的变量异常:

  

/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 新的“

我很确定这是因为form_for的do循环还没有执行(?)......我不确定我对这个问题的处理方法是最好的,但我还是没能找到一个更好的解决方案,动态地向DOM添加表单部分。基本上我需要一种在嵌套表单上动态地向has_many模型添加记录的方法。

有关解决此特定问题的方法或(甚至更好!)清洁解决方案的任何建议都表示赞赏。提前谢谢。

克里斯

1 个答案:

答案 0 :(得分:0)

我建议使用另一个例子而不是这个。我最近也经历了这个过程,最好的实现来自ryan bates:http://github.com/ryanb/complex-form-examples/tree/unobtrusive-jquery-deep

如果您正在使用原型,那么这不是问题,您可以轻松地将实现从jquery更改为原型。