2

I am trying to use the latest form_with to send an Ajax post.

Here is what I did.

<%= form_with url: comments_path, data: { type: :json } do |form| %>
  <%= form.text_area :context, class: "rt-textarea mg-b-10" %>
  <%= form.submit "Submit", class: "btn-m btn-blue, id: "comment-submit" %>
<% end >

<script type="text/javascript">
  jQuery(document).ready(function($) {
    $('#comment-submit').on('ajax:success', function(event, data) {
      console.log('GET RESPONSE!');
    });
  });
</script>

But when I send the form post, the console didn't print GET RESPONSE!. I checked the Network tab in google developer console, it did send the JSON object back. Is there anything I miss?

2 Answers 2

2

Turns out, I put the id in the wrong place.

I should put id in form_with tag instead of submit tag.

After I change to this line, it works perfectly.

<%= form_with url: comments_path, data: { type: :json }, id: "add_comment" do |form| %>
....
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are missing end tag. The form_with may be like this:

<%= form_with url: users_path do |form| %>
      <%= form.text_field :email %>
      <%= form.submit %>
 <% end %>

1 Comment

No, I did add the end tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.