我正在创建一个嵌套表单并使用 Ryan Bates Railscast。我正在创建一个使用部分的典型表单。
= form_for(@project, :html => { :class => 'addProjectForm' }) do |f|
...
%p
= f.fields_for :crew_members do |crew|
#fields
= render 'crew_member_fields', :m => crew
= link_to_add_fields "Add Crew Members", f, :crew_members
%p
然后我调用一个助手类,以便能够克隆“字段”类并将其添加到我的表单中。
module ProjectsHelper
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end
end
然后帮助程序类调用一个 javascript 函数来处理细节。
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
我的问题是当我添加
= link_to_add_fields "Add Crew Members", f, :crew_members
行,我得到错误
undefined local variable or method `m' for #<#<Class:0xa763964>:0xa761588>
Extracted source (around line #1):
1: = m.label "Crew Member Name"
2: = m.label "Crew Member Role"
3: %br
4: = m.text_field :name
这使得看起来部分没有获得 builder 变量,但如果我没有
= link_to_add_fields "Add Crew Members", f, :crew_members
行,然后部分显示就好了。我对接下来如何调试感到困惑。很抱歉,这篇文章很长,但我希望你拥有所有信息。谢谢您的帮助。