0

我正在使用 Ryan Bates 的Complex Forms Deep Branch,并尝试将该示例复制为具有两个额外嵌套级别的表单。

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 函数将无法工作。你能看到我错过了什么吗?

4

1 回答 1

-2

您尚未在 application.js 中添加 javascript 代码。

于 2010-11-20T16:13:32.613 回答