0
我正在使用Ryan Bates的Complex Forms Deep Branch,並試圖複製該示例以獲得具有兩個附加嵌套級別的表單。Rails - Railscasts嵌套的複雜形式
SurveyName有許多SurveyQuestions,其中有許多SurveyOptions。
# application_helper (identical to deep branch)
def remove_child_link(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def add_child_link(name, f, method)
fields = new_child_fields(f, method)
link_to_function(name, h("insert_fields(this, \"#{method}\", \"# {escape_javascript(fields)}\")"))
end
def new_child_fields(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_#{method}") do |f|
render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
end
end
# application.js (identical to deep branch)
function insert_fields(link, method, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + method, "g")
$(link).up().insert({
before: content.replace(regexp, new_id)
});
}
function remove_fields(link) {
var hidden_field = $(link).previous("input[type=hidden]");
if (hidden_field) {
hidden_field.value = '1';
}
$(link).up(".fields").hide();
}
在Deep Branch中,項目有許多任務,其中有許多任務。我的_task.html.erb
等效爲:
<div class="fields">
<%= remove_child_link "remove", f %>
<%= f.fields_for :survey_options do |survey_option_form| %>
<%= render :partial => "survey_option", :locals => { :f => survey_option_form } %>
<% end %>
# the above works
<%= add_child_link "Add Option", f, :survey_options %>
# the above line does NOT work
</div>
我希望我已經提供了足夠的信息。對我來說,使用相同的幫助程序代碼add_child_link函數不起作用,這真是神祕莫測。你能看到我失蹤的東西嗎?
我已添加.js代碼。 – sscirrus 2010-11-22 17:32:29
通過你的回答,我在上面丟失了什麼特定的代碼? – sscirrus 2010-12-12 03:35:21