ruby - rails 中如何對子模型進行更新
習慣沉默
習慣沉默 2017-05-17 10:02:06
0
1
1034

例如 父模型 文章

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

父Model

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

子Model

class Annotation < ApplicationRecord
  belongs_to :code_snippet
end

更新子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! 也許能看到一些問題

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!