如何使用jQuery以嵌套形式生成2个新的关联对象?

问题描述 投票:1回答:2

我有一个名为Pair的模型,它具有has_many:questions,而每个Question具有has_one:answer。

我一直关注此railscast on creating nested forms,但是我想在单击“添加问题”链接时同时生成“问题”字段和“答案”字段。

遵循了railscast之后,这就是我所拥有的:

.. javascripts / common.js.coffee:

window.remove_fields = (link)->
  $(link).closest(".question_remove").remove()

window.add_fields = (link, association, content)->
  new_id = new Date().getTime()
  regexp = new RegExp("new_" + association, "g")
  $(link).before(content.replace(regexp, new_id))

application_helper.rb:

def link_to_add_fields(name, f, association)
   new_object = f.object.class.reflect_on_association(association).klass.new
   fields = f.simple_fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
     render(association.to_s.singularize + "_fields", :f => builder)
   end
   link_to_function(name, "window.add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", class: "btn btn-inverse")
end

views / pairs / _form.html.erb:

<%= simple_form_for(@pair) do |f| %>
  <div class="row">
    <div class="well span4">
      <%= f.input :sys_heading, label: "System Heading", placeholder: "required", input_html: { class: "span4" } %>
      <%= f.input :heading, label: "User Heading", input_html: { class: "span4" } %>
      <%= f.input :instructions, as: :text, input_html: { class: "span4 input_text" } %>
    </div>
  </div>

  <%= f.simple_fields_for :questions do |builder| %>
    <%= render 'question_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "<i class='icon-plus icon-white'></i> Add Another Question".html_safe, f, :questions %>
  <%= f.button :submit, "Save Pair", class: "btn btn-success" %>
<% end %>

_ question_fields.html.erb部分:

<div class="question_remove">
  <div class="row">
    <div class="well span4">
      <%= f.input :text, label: "Question", input_html: { class: "span4" }, placeholder: "your question...?" %>

      <%= f.simple_fields_for :answer do |builder| %>
        <%= render 'answer_fields', f: builder %>
      <% end %>
    </div>
  </div>
</div>

_ answer_fields.html.erb部分:

<%= f.input :text, label: "Answer", input_html: { class: "span4" }, placeholder: "your answer" %>
<%= link_to_function "remove", "remove_fields(this)", class: "float-right" %>

我对reflect_on_association部分特别困惑,例如,在那里调用.new如何创建关联?我通常需要使用.build

也为has_one我使用.build_answer而不是answers.build-那么这对于jQuery部分意味着什么?

jquery ruby-on-rails ruby-on-rails-3 associations nested-forms
2个回答
1
投票

一种检查方法是在Rails控制台中运行帮助程序脚本。

new_object = f.object.class.reflect_on_association(association).klass.new

您正在获取Form对象并找到其所属的类(f.object.class-在这种情况下为[[pair),然后使用reflect_on_association(association)-返回已命名关联的AssociationReflection对象(从参数(即:questions)中作为符号传递。

您可以使用reflect_on_all_associations查看每个类的所有这些AssociationReflection对象。

此后,您需要AssociationReflection代表的类(这就是为什么使用klass而不是类的原因-然后您创建该类的新实例。

为什么不使用build _#{association_name}或#{association_name} .builds,而不是new可能是因为脚本不知道关联是has_one还是HABTM,直到它已经检索到一个实例为止。关联的对象,附加的逻辑不值得。

http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html

通过修订的铁路广播使用的方式生成关联对象的方式略有不同:但是思想是相同的(在这种情况下,您要在表单对象上发送-调用-方法,然后生成相关的类)]

new_object = f.object.send(association).klass.new

关于在帮助程序中使用两个模型进行类似处理?

application_helper.rb

def link_to_add_nested_fields(name, f, association, nested_association) new_object = f.object.class.reflect_on_association(association).klass.new new_object.send(nested_association).new fields = f.simple_fields_for(association, new_object, :child_index => "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f => builder) end link_to_function(name, "window.add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", class: "btn btn-inverse") end

例如

<%= link_to_add_fields "<i class='icon-plus icon-white'></i> Add Another Question".html_safe, f, :questions, :answer %>


0
投票
下面的作品。我没有使用

form_for代替simple_form_forfields_for代替了[[simple_fields_for。感谢粘贴

def link_to_add_nested_fields(name,f,association,nested_association)

new_object = f.object.class.reflect_on_association(association).klass.new

id = new_object.object_id#需要索引,这一点很重要。在浏览器中检查

nested_new_object = new_object.class.reflect_on_association(nested_association).klass.new

fields = f.fields_for(关联,new_object,:child_index => id)做| builder |

builder.fields_for(nested_association, nested_new_object, :child_index => id) do |builder1| render(association.to_s.singularize + "_fields", association.to_s.singularize.to_sym => builder) + render(nested_association.to_s.singularize + "_fields", nested_association.to_s.singularize.to_sym => builder1) end

结束

link_to(name,'javascript:void(0)',类:“ btn btn-primary add_fields col-lg-8”,样式:“ width:auto; margin-bottom:10px;”,数据:{id: id,字段:fields.gsub(“ \ n”,“”)})]

end

© www.soinside.com 2019 - 2024. All rights reserved.