For example, parent model article
子模型 评论
一篇文章有许多评论,但是我发现评论写错了 需要对评论进行修改,这个需要如何做呢?
ParentModel
class CodeSnippet < ApplicationRecord
has_many :annotations, dependent: :destroy
accepts_nested_attributes_for :annotations ,update_only: true ,reject_if: :all_blank, allow_destroy: true
end
SubModel
class Annotation < ApplicationRecord
belongs_to :code_snippet
end
Update child Model form
<%= 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 %>
No update after clicking
Controller needs to do the corresponding processing, try to use save when saving code_snippet! Maybe you can see some problems