我正在尝试弄清楚如何将编辑链接呈现到嵌套的表单。
我有名为“项目和潜在用途”的模型。这些协会是:
项目
has_many :potential_uses
accepts_nested_attributes_for :potential_uses, reject_if: :all_blank, allow_destroy: true
潜在用途
belongs_to :project
belongs_to :user
在我的项目显示页面中,我从我的潜在用途 View 文件夹中渲染了一个 View 部分。在该 View 中,我呈现一个链接以允许编辑内容。
<% @project.potential_uses.each do |pu| %>
<p><%= pu.comment %></p>
<%= link_to 'Edit', edit_project_potential_use_path(@project, pu), f: f %>
在我的潜在用途 View 文件夹中,我有一个编辑 View ,其中:
<%= render 'potential_uses_third_party_comments' %>
在我可能使用第三方评论的部分中,我有:
<%= simple_form_for [ @project, @potential_use ] do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :comment, :label => 'Comment ', :input_html => {:rows => 5} %>
</div>
<div class="form-actions">
<%= f.button :submit, "Submit", :class => 'formsubmit' %>
</div>
这件事是错误的,因为当我保存它并尝试使用它时,我收到一条错误消息:
undefined local variable or method `f' for #<#<Class:0x007f8cb2f30188>:0x007f8cd171f448>
我从其他帖子中了解到,这个错误可以通过在编辑链接末尾添加 f: f 来解决。我已经有了。
有人能看出我做错了什么吗?
最佳答案
您可以使用rails fields_for方法来渲染嵌套。
<%= f.fields_for :potential_uses do |pu| %>
<%= render 'potential_uses_third_party_comments', f: pu %>
<% end %>
<%= link_to_add_association '+ Add Potential Use(s)', f, :potential_uses, partial: "potential_uses_third_party_comments", class: "btn btn-sm btn-primary" %>
关于ruby-on-rails - Rails - 如何在编辑模式下渲染嵌套表单部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39090740/