Heim  >  Fragen und Antworten  >  Hauptteil

Ruby – So aktualisieren Sie Untermodelle in Rails

Beispielartikel zum Elternmodell

子模型  评论
一篇文章有许多评论,但是我发现评论写错了 需要对评论进行修改,这个需要如何做呢?

Elternmodell

class CodeSnippet < ApplicationRecord
  has_many :annotations, dependent: :destroy
  accepts_nested_attributes_for :annotations ,update_only: true ,reject_if: :all_blank, allow_destroy: true
end

Untermodell

class Annotation < ApplicationRecord
  belongs_to :code_snippet
end

Untermodellformular aktualisieren

<%= 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 %>

Keine Aktualisierung nach dem Klicken

習慣沉默習慣沉默2686 Tage vor1037

Antworte allen(1)Ich werde antworten

  • 滿天的星座

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

    controller 要做对应的处理, 尝试在保存code_snippet 的时候 使用save! 也许能看到一些问题

    Antwort
    0
  • StornierenAntwort