Asynchronization of message transmission
Let the event fire when the form is submitted
![スクリーンショット 2020-07-03 22.12.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/663122/2ebf9493-5bea-a096-28ce-e1eec7a575a4.png )
I will explain this description.
In $ (*****), describe the class name of form.
Describe the event name in .on (***** ,.
In e.preventDefaul, the default event is stopped for asynchronous communication.
## Let's use Ajax to get messages # create to work when the event fires
![スクリーンショット 2020-07-03 22.17.55.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/663122/31442d72-7443-7f06-543c-55a057d5ebb0.png )
This in this description contains information about the Form that fired the event.
$ (this) .attr ('action'); is getting the path of Form information.
# Let's save the message with the #create action of the messages controller and use respond_to to return the response to the JSON format request
![スクリーンショット 2020-07-03 22.21.45.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/663122/f2c3d5c8-1b01-bc33-6ac9-d3442b79c392.png )
if @message.save
Saves the information sent in the request
respond_to do |format|
format.json
Returning in json method
## Other output
![スクリーンショット 2020-07-03 22.25.20.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/663122/a7daee69-d21a-e7ec-3b54-8852db29a3cd.png )
HTML is added to the class name MessageField with append (html)
$(".submit-btn").prop('disabled', false);
If you press the send button once, you will not be able to press it unless you reload it.
By writing prop ('disabled', false); in the submit button class, you can post without loading.
## Processing when asynchronous failure
![スクリーンショット 2020-07-03 22.31.17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/663122/57ec4196-c18e-6548-07ba-538cf267fb4f.png )
If it fails, it will alert you of the error.
Used after Done