[英]Problem using form builder & DOM manipulation in Rails with multiple levels of nested partials
我在Rails中使用帶有動態表單構建器代碼(來自github上的“復雜表單示例”代碼 )的嵌套部分代碼時遇到問題。 我的頂層視圖為“新”(嘗試生成模板):
<% 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> </div><div id="container">
<%= link_to 'Add a transaction', '#transaction', :class => "add_nested_item", :rel => "transactions" %>
</div>
<div> </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
_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'
我很確定這是因為form_for的do循環尚未執行(?)...我不確定我對這個問題的解決方法是最好的,但是我還沒有找到更好的方法用於將表單部分動態添加到DOM的解決方案。 基本上,我需要一種在嵌套表單上動態將記錄添加到has_many模型的方法。
任何解決此特定問題或(甚至更好!)解決方案的建議都將受到贊賞。 提前致謝。
克里斯
我建議使用另一個示例代替此示例。 我最近也經歷了這個過程,最好的實現是來自ryan bates: http : //github.com/ryanb/complex-form-examples/tree/unobtrusive-jquery-deep
如果您使用的是原型,那么這不是問題,您可以輕松地將實現從jquery更改為原型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.