Rails 5.1.2 - form_with doesn't show data-remote = "true" in html

Rails 5.1.2:

I am trying to create an AJAX form using form_with according to the Rails documentation and this GitHub thread .

This code:

<%= form_with url: '/' do |f| %>
<% end %>

      

and in fact this code:

<%= form_with url: '/', remote: true do |f| %>
<% end %>

      

both create this html:

<form action="/" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="..." />
  <input type="hidden" name="authenticity_token" value="..." />
</form>

      

Why doesn't it appear in the HTML data-remote="true"

as the first link I posted indicates that it should and how can I display it?

+3


source to share


4 answers


The default value for is data-remote

configured by option Rails.application.config.action_view.form_with_generates_remote_forms

. By default in Rails 5 this option true

. Searching your entire project looks like you migrated from Rails 4 or smth. else overriding this option.



+3


source


In Rails 5.1.2 By default, form_with

it assumes that your form will use Ajax. You can opt out of this behavior by passing a parameter :local

to form_with

.

<%= form_with url: '/' do |f| %>
<% end %>

      

The above code creates



<form action="/" accept-charset="UTF-8" data-remote="true" method="post">
  <input name="utf8" type="hidden" value="..." />
  <input type="hidden" name="authenticity_token" value="..." />
</form>

      

Also if you want to use the form without ajax you can use below -

<%= form_with url: '/', local: true do |f| %>
<% end %>

      

+1


source


I had the same problem with a Rails 5.1.4 application. The local: false

problem is fixed with the help .

<%= form_with url: '/', local: false do |f| %>
<% end %>

      

+1


source


This will happen in Rails 5.1

With my config in new_framework_defaults_5_1.rb like this:

# Make `form_with` generate non-remote forms.
Rails.application.config.action_view.form_with_generates_remote_forms = false

      

And in my view file I use like this:

= form_with(model: your_model , url: your_path, method: :post, local: false) do |f|
....your input here...

      

Now your html code will generate data-remote: true on your form.

0


source







All Articles