使用“link_to_add_fields 帮助器”自动构建嵌套模型表单

发布于 2024-09-10 19:05:51 字数 1540 浏览 6 评论 0原文

我已经从 RailsCasts 嵌套模型第 2 部分示例中创建了一个嵌套模型,并让它与 Rails 3 和 JQuery 一起使用。目前,用户可以创建一个新项目并单击链接来添加新任务,并在每个新任务中单击一个链接,允许他们为每个任务添加新分配。下面是创建新任务的链接(创建新作业的链接类似):

        <p><%= link_to_add_fields "Add task", f, :task %></p>

指的是这个助手:

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

它的工作原理如下: 单击该链接,通过 JQuery 添加 html,该 JQuery 显示任务名称的表单字段和允许用户向任务添加新分配的链接。

我想要做的是将作业表单显示在任务名称字段旁边 - 这样用户就不需要单击其他链接来为每个任务添加新作业。当页面首次通过项目控制器加载时,我可以完成此操作:

    1.times do
   task = @product.tasks.build
   1.times { task.assignments.build}
end

但是,因为我使用 JQuery 添加和删除 HTML,所以在“添加”新任务时不会调用控制器,也不会构建表单。

创建新任务字段时,是否有办法自动(从任务部分内)“构建”嵌套分配字段?

这是我在用户单击表单中的“新任务”链接时创建的任务表单的部分内容:

<div class = "fields">
<%= f.label :name, "Task name"%>
<%= link_to_remove_fields "remove", f %><br />
<%= f.text_field :name %>



<% f.fields_for :assignments do |builder| %>
    <%= render 'assignment_fields', :f => builder %>
<% end %>

<p><%= link_to_add_fields "Add assignments", f, :assignments %></p>

Ive created a nested model form the RailsCasts Nested Model Form Part 2 example and have it working with Rails 3 and JQuery. Currently, a user can create a new project and click a link to add new tasks, and within each new task, click a link that will allow them to add new assignments to each task. Here's the link to create a new task (the link to create new assignments is similar):

        <p><%= link_to_add_fields "Add task", f, :task %></p>

That refers to this helper:

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

It works like this: By clicking the link, html is added through JQuery that displays a form field for the task's name and a link that allows a user to add new assignments to the task.

What I'd like to do is have the assignment forms appear alongside the task name field - so that the user does not need to click an additional link to add a new assignments to each task. I can accomplish this when the page first loads through the project controller:

    1.times do
   task = @product.tasks.build
   1.times { task.assignments.build}
end

However, because Im using JQuery to add and remove HTML, the controller won't be called and the forms won't be build when 'adding' new tasks.

Is there a way to 'build' the nested assignment fields automatically (from within the task partial) when the new task field is created?

Here's my partial for the task form that is created when a user clicks the 'new task' link in the form:

<div class = "fields">
<%= f.label :name, "Task name"%>
<%= link_to_remove_fields "remove", f %><br />
<%= f.text_field :name %>



<% f.fields_for :assignments do |builder| %>
    <%= render 'assignment_fields', :f => builder %>
<% end %>

<p><%= link_to_add_fields "Add assignments", f, :assignments %></p>

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

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

发布评论

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

评论(2

挽手叙旧 2024-09-17 19:05:52

我有类似的情况,我目前(像你一样)正在寻找解决方案。
从Rails 3源代码中我发现:

activerecord/lib/active_record/nested_attributes.rb:

# === One-to-many
#
# Consider a member that has a number of posts:
#
#   class Member < ActiveRecord::Base
#     has_many :posts
#     accepts_nested_attributes_for :posts
#   end
#
# You can now set or update attributes on an associated post model through
# the attribute hash.
#
# For each hash that does _not_ have an <tt>id</tt> key a new record will
# be instantiated, unless the hash also contains a <tt>_destroy</tt> key
# that evaluates to +true+.

我希望这能给你一些想法。

I have a similar situation and I am currently (like you) looking for a solution.
From Rails 3 source I found:

activerecord/lib/active_record/nested_attributes.rb:

# === One-to-many
#
# Consider a member that has a number of posts:
#
#   class Member < ActiveRecord::Base
#     has_many :posts
#     accepts_nested_attributes_for :posts
#   end
#
# You can now set or update attributes on an associated post model through
# the attribute hash.
#
# For each hash that does _not_ have an <tt>id</tt> key a new record will
# be instantiated, unless the hash also contains a <tt>_destroy</tt> key
# that evaluates to +true+.

I hope this gives you ideas.

貪欢 2024-09-17 19:05:52

创建任务表单后,您可以使用 jQuery 模拟单击​​“添加分配”链接。这样你就根本不依赖 Rails 控制器。

after_create_handler = function() { $('assignment add link identifier').click() }

You can simulate a click of the add assignments link with jQuery once the task form is created. That way you don't depend on a rails controller at all.

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