Rails - Railscast 嵌套复杂形式

发布于 2024-10-03 03:34:46 字数 2047 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2024-10-10 03:34:46

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

You have not added the javascript code in your application.js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文