登录

ruby - rails 中如何对子模型进行更新

例如 父模型 文章

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

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

点击后并没有更新

# Ruby
習慣沉默 習慣沉默 2507 天前 890 次浏览

全部回复(1) 我要回复

  • 滿天的星座

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

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

    回复
    0
  • 取消 回复 发送