Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery以嵌套形式生成2个新的关联对象?_Jquery_Ruby On Rails_Ruby On Rails 3_Associations_Nested Forms - Fatal编程技术网

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

如何使用jQuery以嵌套形式生成2个新的关联对象?,jquery,ruby-on-rails,ruby-on-rails-3,associations,nested-forms,Jquery,Ruby On Rails,Ruby On Rails 3,Associations,Nested Forms,我有一个叫做配对的模型,它有很多:问题,每个问题都有一个:答案 我一直在关注这一点,但是我想在单击“添加问题”链接时生成一个问题字段和它的答案字段 遵循铁路路线后,我得到的是: ..javascripts/common.js.coffee: window.remove_fields = (link)-> $(link).closest(".question_remove").remove() window.add_fields = (link, association, conten

我有一个叫做配对的模型,它有很多:问题,每个问题都有一个:答案

我一直在关注这一点,但是我想在单击“添加问题”链接时生成一个问题字段和它的答案字段

遵循铁路路线后,我得到的是:

..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
视图/对/_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 %>

_问题_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>
<%= f.input :text, label: "Answer", input_html: { class: "span4" }, placeholder: "your answer" %>
<%= link_to_function "remove", "remove_fields(this)", class: "float-right" %>

_回答_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>
<%= f.input :text, label: "Answer", input_html: { class: "span4" }, placeholder: "your answer" %>
<%= link_to_function "remove", "remove_fields(this)", class: "float-right" %>

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


同样对于has\u one,我使用
.build\u answer
而不是
answers.build
-那么这对jQuery部分意味着什么呢?

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

new_object = f.object.class.reflect_on_association(association).klass.new
您使用表单对象并找到它所用于的类(f.object.class-在本例中为pair),然后使用reflect\u on\u association(association)-返回命名关联的AssociationReflection对象(作为参数的符号传递,即:问题)

通过使用reflect\u on\u all\u关联,可以查看每个类的所有这些AssociationReflection对象

在此之后,您需要AssociationReflection表示的类(这就是为什么您使用klass而不是class),然后创建该类的新实例

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

顺便说一句,修订后的railcast使用了一种稍微不同的方法来生成关联对象:但其思想是相同的(在本例中,您在表单对象上发送-调用-一个方法,然后生成相关类)

至于在助手中使用两个模型来制作这条线

应用程序\u 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
例如


检查此问题的一种方法是在Rails控制台中运行helper脚本

new_object = f.object.class.reflect_on_association(association).klass.new
您使用表单对象并找到它所用于的类(f.object.class-在本例中为pair),然后使用reflect\u on\u association(association)-返回命名关联的AssociationReflection对象(作为参数的符号传递,即:问题)

通过使用reflect\u on\u all\u关联,可以查看每个类的所有这些AssociationReflection对象

在此之后,您需要AssociationReflection表示的类(这就是为什么您使用klass而不是class),然后创建该类的新实例

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

顺便说一句,修订后的railcast使用了一种稍微不同的方法来生成关联对象:但其思想是相同的(在本例中,您在表单对象上发送-调用-一个方法,然后生成相关类)

至于在助手中使用两个模型来制作这条线

应用程序\u 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
例如


下面的方法有效。我没有用表单代替简单表单,也没有用字段代替简单字段。感谢粘贴

定义链接到添加嵌套字段(名称、f、关联、嵌套关联)

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

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

嵌套的\u new\u object=新建的\u object.class.reflect\u on\u association(嵌套的\u association).klass.new

fields=f.fields\u for(关联,新对象,:child\u index=>id)do | 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
结束

链接到(名称,'javascript:void(0)”,类:“添加字段”,数据:{id:id,fields:fields.gsub(“\n”,”)})


结束

下面的工作。我没有用表单代替简单表单,也没有用字段代替简单字段。感谢粘贴

定义链接到添加嵌套字段(名称、f、关联、嵌套关联)

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

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

嵌套的\u new\u object=新建的\u object.class.reflect\u on\u association(嵌套的\u association).klass.new

fields=f.fields\u for(关联,新对象,:child\u index=>id)do | 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
结束

链接到(名称,'javascript:void(0)”,类:“添加字段”,数据:{id:id,fields:fields.gsub(“\n”,”)})


结束

签出谢谢我会看一看,但我仍然想了解如何在没有gem的情况下完成它,以便更好地理解Rails/JavaScript。签出谢谢我会看一看,但我仍然想了解如何在没有gem的情况下完成它,以便更好地理解Rails/JavaScript。
new\u object=f、 object.class.reflect_on_association(association).klass.new
基本上与在这种情况下编写
new_object=Question.new
相同?问题是,现在当我单击“添加问题”链接时,只会生成一个新的问题字段(显然),但我想从问题模型和An生成一个新对象