form_withでformを実装してsubmitするとモデルから最もらしいアクションを推測して自動的にページを更新してくれるみたいなのですが、どのような仕組みなのか気になります。
例にこのコード
submitすると自動的にupdateアクションが実行されます。
モデルpostが空っぽかそうでないかで、createとupdateを区別しているといった感じでしょうか?
よろしくお願いします。
参考サイト https://techracho.bpsinc.jp/hachi8833/2017_05_01/39502
<%= form_with(model: post, local: true) do |form| %> <% if post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% post.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= form.label :title %> <%= form.text_field :title, id: :post_title %> </div> <div class="field"> <%= form.label :body %> <%= form.text_area :body, id: :post_body %> </div> <div class="field"> <%= form.label :description %> <%= form.text_area :description, id: :post_description %> </div> <div class="actions"> <%= form.submit %> </div> <% end %>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/28 05:47
2018/04/28 05:57