问题描述
例如 父模型 文章
子模型 评论一篇文章有许多评论,但是我发现评论写错了 需要对评论进行修改,这个需要如何做呢?
父Model
class CodeSnippet < ApplicationRecord has_many :annotations, dependent: :destroy accepts_nested_attributes_for :annotations ,update_only: true ,reject_if: :all_blank, allow_destroy: trueend
子Model
class Annotation < ApplicationRecord belongs_to :code_snippetend
更新子Model表单
<%= form_for(@code_snippet) do |f| %> <%= f.fields_for :annotation,method: :patch do |builder| %><p> <%= builder.label :user %><br> <%= builder.text_field :user %></p><p> <%= builder.label :line %><br> <%= builder.text_field :line %></p><p> <%= builder.label :body %><br> <%= builder.text_area :body %></p><p> <%= builder.submit %></p> <% end %><% end %>
点击后并没有更新
问题解答
回答1:controller 要做对应的处理, 尝试在保存code_snippet 的时候 使用save! 也许能看到一些问题