Rails-Railscasts嵌套了复杂的表单 [英] Rails - Railscasts nested complex forms
问题描述
我正在使用Ryan Bates的复杂表格深层分支,并尝试将该示例复制为具有两个附加嵌套级别的表单.
I am using Ryan Bates' Complex Forms Deep Branch, and trying to replicate that example for a form that has two additional nested levels.
SurveyName有许多SurveyQuestions,其中有许多SurveyOptions.
SurveyName has many SurveyQuestions, which have many 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中,Project有很多任务,有很多任务.我的_task.html.erb
等效项是:
In Deep Branch, Project has many tasks, which have many assignments. My _task.html.erb
equivalent is:
<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函数将无法正常工作.可以看到我想念的东西吗?
I hope I've given enough information. It is mystifying to me that with the same helper code the add_child_link function wouldn't work. Can you see what I'm missing?
推荐答案
您尚未在application.js中添加javascript代码.
You have not added the javascript code in your application.js.
这篇关于Rails-Railscasts嵌套了复杂的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!