search

Home  >  Q&A  >  body text

ruby - how to update submodels in rails

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

習慣沉默習慣沉默2781 days ago1109

reply all(1)I'll reply

  • 滿天的星座

    滿天的星座2017-05-17 10:04:06

    Controller needs to do the corresponding processing, try to use save when saving code_snippet! Maybe you can see some problems

    reply
    0
  • Cancelreply