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

例如 父模型 文章

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

父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学习者快速成长!