如果使用 jQuery 添加字段,Rails 嵌套表单属性不会保存

发布于 2024-08-28 05:09:43 字数 1611 浏览 6 评论 0原文

我有一个带有嵌套表单的 Rails 表单。我使用 Ryan Bates 嵌套表单和 jquery 教程,并且就动态添加新字段而言,它工作得很好。

但是当我提交表单时,它不会保存任何关联的属性。但是,如果在表单加载时构建部分,则它会很好地创建属性。我无法弄清楚 JavaScript 中未传递的内容未能传达需要保存表单对象的信息。

任何帮助都会很棒。

class Itinerary < ActiveRecord::Base
  accepts_nested_attributes_for :trips
end

行程/new.html

<% form_for ([@move, @itinerary]), :html => {:class => "new_trip" } do |f| %>

  <%= f.error_messages %>
  <%= f.hidden_field :move_id, :value => @move.id %>
  <% f.fields_for :trips do |builder| %>
    <%= render "trip", :f => builder %>
  <% end %>
<%= link_to_add_fields "Add Another Leg to Your Trip", f, :trips %>

<p><%= f.submit "Submit" %></p>

<% end %>

application_helper.rb

 def link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
 end

 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, :f => builder)
  end
  link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
 end

application.js

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));
}

I have a rails form with a nested form. I used Ryan Bates nested form with jquery tutorial and I have it working fine as far as adding the new fields dynamically.

But when I go to submit the form it does not save any of the associated attributes. However if the partial builds when the form loads it creates the attribute just fine. I can not figure out what is not being passed in the javascript that is failing to communicate that the form object needs to be saved.

Any help would be great.

class Itinerary < ActiveRecord::Base
  accepts_nested_attributes_for :trips
end

itinerary/new.html

<% form_for ([@move, @itinerary]), :html => {:class => "new_trip" } do |f| %>

  <%= f.error_messages %>
  <%= f.hidden_field :move_id, :value => @move.id %>
  <% f.fields_for :trips do |builder| %>
    <%= render "trip", :f => builder %>
  <% end %>
<%= link_to_add_fields "Add Another Leg to Your Trip", f, :trips %>

<p><%= f.submit "Submit" %></p>

<% end %>

application_helper.rb

 def link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
 end

 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, :f => builder)
  end
  link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
 end

application.js

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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ι不睡觉的鱼゛ 2024-09-04 05:09:43

您是否尝试过添加:

class Itinerary < ActiveRecord::Base
  attr_accessible :move_id, :trips_attributes
  accepts_nested_attributes_for :trips
end

当然,您还需要为您的行程模型添加字段。如果不知道这些,我就会猜测它们是什么。

Have you tried adding:

class Itinerary < ActiveRecord::Base
  attr_accessible :move_id, :trips_attributes
  accepts_nested_attributes_for :trips
end

Of course you would then need to add the fields for your Itinerary model also. Without knowing those I would be guessing what they are.

生生漫 2024-09-04 05:09:43

我和你有完全相同的问题。 删除该行来修复它。

validates_associated :parent_model

我通过从嵌套模型(在您的例子中是 Trip 模型)中

I had exactly the same problem as you. I fixed it by removing the line

validates_associated :parent_model

from the nested model (in your case the Trip model).

仙女山的月亮 2024-09-04 05:09:43

我有类似的问题。从父模型中删除 attr_accessible 行对我来说很有效。我发现您的迭代模型已经没有该行,因此这可能对您没有帮助;但可能会帮助别人。

I was having similar issue. Removing the attr_accessible line from parent model did the trick for me. I see that your Itenerary model is already devoid of that line, so this might not help you; but might help someone else.

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