質問編集履歴

2

該当ファイルのコードを修正しました

2019/01/13 15:45

投稿

Eltk
Eltk

スコア51

test CHANGED
File without changes
test CHANGED
@@ -42,67 +42,7 @@
42
42
 
43
43
  ```ここに言語を入力
44
44
 
45
- $(function(){
46
-
47
- function buildHTML(message){
48
-
49
- console.log(message);
50
-
51
- var html = `
52
-
53
- <div class = "main-content_body_list-message">
54
-
55
- <div class = "main-content_body_list-message-name">
56
-
57
- ${message.user_name}
58
-
59
- </div>
60
-
61
- <div class = "main-content_body_list-message-time">
62
-
63
- ${message.created_at}
64
-
65
- </div>
66
-
67
- <div class = "main-content_body_list-message-text">
68
-
69
- <p>${message.content}</p>
70
-
71
- <img src="${message.image}">
72
-
73
- </div>
74
-
75
- </div>`
76
-
77
- return html;
78
-
79
- }
80
-
81
- $('.new_message').on('submit', function(e){
82
-
83
- e.preventDefault();
84
-
85
- var formData = new FormData(this);
86
-
87
- var url = $(this).attr('action')
88
-
89
- $.ajax({
90
-
91
- url: url,
92
-
93
- type: 'POST',
94
-
95
- data: formData,
96
-
97
- dataType: 'json',
98
-
99
- processData: false,
100
-
101
- contentType: false
102
-
103
- })
104
-
105
- .done(function(data){
45
+ .done(function(data){
106
46
 
107
47
  var html = buildHTML(data);
108
48
 
@@ -112,7 +52,7 @@
112
52
 
113
53
  $('.main-content_footer_body_message-area').val('')
114
54
 
115
- $('.upload.label').val('')
55
+ $('#form')[0].reset();
116
56
 
117
57
  $('.main-content_body_list').animate({scrollTop: $('.main-content_body_list')[0].scrollHeight}, 'fast');
118
58
 
@@ -132,4 +72,36 @@
132
72
 
133
73
 
134
74
 
75
+
76
+
77
+
78
+
79
+ HAMLファイル
80
+
81
+
82
+
83
+
84
+
85
+ .main-content_footer
86
+
87
+ .main-content_footer_body
88
+
89
+ =form_for [@group, @message] do |f|
90
+
91
+ .main-content_footer_body_message
92
+
93
+ =f.text_field :content, class: 'main-content_footer_body_message-area', placeholder: 'type a message'
94
+
95
+ %label.upload-label{for: "upload-icon"}
96
+
97
+ %i.fa.fa-image
98
+
99
+ = f.file_field :image, id: "upload-icon"
100
+
101
+ .main-content_footer_body_submit-btn
102
+
103
+ =f.submit 'Send', class: 'main-content_footer_body_submit-btn-area'
104
+
105
+
106
+
135
107
  ```

1

コードを写真でなく、コードブロックで記述しなおしました。

2019/01/13 15:45

投稿

Eltk
Eltk

スコア51

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,101 @@
35
35
 
36
36
 
37
37
  ※非同期通信のコードを添付させていただきます。![イメージ説明](c7e260bfb073021935214fb137f0016c.png)
38
+
39
+
40
+
41
+
42
+
43
+ ```ここに言語を入力
44
+
45
+ $(function(){
46
+
47
+ function buildHTML(message){
48
+
49
+ console.log(message);
50
+
51
+ var html = `
52
+
53
+ <div class = "main-content_body_list-message">
54
+
55
+ <div class = "main-content_body_list-message-name">
56
+
57
+ ${message.user_name}
58
+
59
+ </div>
60
+
61
+ <div class = "main-content_body_list-message-time">
62
+
63
+ ${message.created_at}
64
+
65
+ </div>
66
+
67
+ <div class = "main-content_body_list-message-text">
68
+
69
+ <p>${message.content}</p>
70
+
71
+ <img src="${message.image}">
72
+
73
+ </div>
74
+
75
+ </div>`
76
+
77
+ return html;
78
+
79
+ }
80
+
81
+ $('.new_message').on('submit', function(e){
82
+
83
+ e.preventDefault();
84
+
85
+ var formData = new FormData(this);
86
+
87
+ var url = $(this).attr('action')
88
+
89
+ $.ajax({
90
+
91
+ url: url,
92
+
93
+ type: 'POST',
94
+
95
+ data: formData,
96
+
97
+ dataType: 'json',
98
+
99
+ processData: false,
100
+
101
+ contentType: false
102
+
103
+ })
104
+
105
+ .done(function(data){
106
+
107
+ var html = buildHTML(data);
108
+
109
+ console.log(data);
110
+
111
+ $('.main-content_body_list').append(html);
112
+
113
+ $('.main-content_footer_body_message-area').val('')
114
+
115
+ $('.upload.label').val('')
116
+
117
+ $('.main-content_body_list').animate({scrollTop: $('.main-content_body_list')[0].scrollHeight}, 'fast');
118
+
119
+ $(".main-content_footer_body_submit-btn-area").prop("disabled", false);
120
+
121
+ })
122
+
123
+ .fail(function(){
124
+
125
+ alert('error');
126
+
127
+ })
128
+
129
+ });
130
+
131
+ });
132
+
133
+
134
+
135
+ ```