How do I write a rails 6 `form_with` block to make a JSON call?

23 views Asked by At

Here's the code:

<%= form_with(url: invitations_path, remote: true, 'data-type' => 'json') do |f| %>
<%= hidden_field_tag :from, user_id %>
<%= hidden_field_tag :to, secret_crush_id %>
<%= f.submit "Send a valentine?", class: "btn btn-secondary" %>
<% end %>

Here's the generated HTML:

 <form action="/invitations" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="[OMITTED]" autocomplete="off" />
  <input type="hidden" name="from" id="from" value="1" autocomplete="off" />
  <input type="hidden" name="to" id="to" value="2" autocomplete="off" />
  <input type="submit" name="commit" value="Send a valentine?" class="btn btn-secondary" data-disable-with="Send a valentine?" />
</form> 

Nothing in the generated HTML suggests (to me at least) that this should be an ajax call, and in fact the controller reports it has format .html.

Also, this form is coming from a page based on a different rails model, in case that's a factor.

What do I need to do to make this an ajax call?

0

There are 0 answers