DEV Community

not-a-patch
not-a-patch

Posted on

`form_with` option change from `local: false` to `local: true`

Introduction

form_with was introduced in Rails 5.1 to avoid the confusion of having form_for and form_tag having similar roles.

form_with, was to have by default forms submitted with AJAX, xhr, rather than the standard HTTP request. The difference is that with a HTTP request, the current window is cleared, and the server response is loaded into the window. While an AJAX request, the current window is unaffected and the page can be updated depending on the results of the request.

Rails changes to form_with

Rails 5.1 documentation of form_with options described the situation as:

:local - By default form submits are remote and unobstrusive XHRs. Disable remote submits with local: true.

However, by Rails 6.1 the situation has reversed as form_with was now local by default. This is described in the documentation as::

:local - By default form submits via typical HTTP requests. Enable remote and unobtrusive XHRs submits with local: false. Remote forms may be enabled by default by setting config.action_view.form_with_generates_remote_forms = true.

Verify

With all this confusion, it's good to verify that what you think you're doing is actually happening. In the code we add local: false meaning this is a remote form to be submitted by AJAX.

Example Code (from 5.1 release notes)

<%= form_with(model: @article, local: false) do |form| %>
  ...
<% end %>
Enter fullscreen mode Exit fullscreen mode

Produced code

If we view the HTML of the page the produced code includes data-remote="true". We have an AJAX form.

<form action="/articles" ... data-remote="true">
  ...
</form>
Enter fullscreen mode Exit fullscreen mode

Motivation for writing

I keep getting confused about what happened when, and I'm writing this as reference to me. I do like the api dock which makes it easier to see what happened. Obviously, if I've misunderstood something, please feel to correct me. I wouldn't want to make the situation any worse!!

Reading

2.8 Unification of form_for and form_tag into form_with

Top comments (0)