Rails - Railscast 嵌套复杂形式
我正在使用 Ryan Bates 的 复杂形式深分支,并尝试为具有两个附加嵌套级别的表单复制该示例。
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中,Project有很多任务,有很多作业。我的 _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 函数将无法工作。你能看到我错过了什么吗?
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 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();
}
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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未在 application.js 中添加 javascript 代码。
You have not added the javascript code in your application.js.