質問編集履歴
1
ソースの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -79,3 +79,187 @@
|
|
79
79
|
|
80
80
|
|
81
81
|
その場合、今回のように異なるcontroller間の異なるactionでajaxの処理をする場合、レスポンスを受け取るjsからどのようにレスポンスデータをview側に追加すれば良いのでしょうか?
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
【追記】各ページのソースは以下になります。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
`posts/show`のview(views/postsの中にあります)
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```html
|
94
|
+
|
95
|
+
h1.title 投稿詳細
|
96
|
+
|
97
|
+
.post_container
|
98
|
+
|
99
|
+
p.post_user_name
|
100
|
+
|
101
|
+
= link_to @post.user.user_name, user_path(@post.user_id), class:'box_link user_link'
|
102
|
+
|
103
|
+
= image_tag @post.post_image_url.url,alt:'post'
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
h1.post_title #{@post.post_title}
|
108
|
+
|
109
|
+
p.post_text #{@post.post_text}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
.post_info.clearfix
|
114
|
+
|
115
|
+
p.view_count
|
116
|
+
|
117
|
+
span #{@post.view_count}
|
118
|
+
|
119
|
+
'view
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
p.comment_count
|
124
|
+
|
125
|
+
| コメント
|
126
|
+
|
127
|
+
span#comment_count_num #{@post.comments_count}
|
128
|
+
|
129
|
+
'件
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
time.post_time = time_ago_in_words(@post.created_at) +'前'
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
- if @post.comments_count > 5
|
138
|
+
|
139
|
+
p#read_more もっと読む...
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
- @comments.each do |comment|
|
144
|
+
|
145
|
+
.comment_wrapper.clearfix#0
|
146
|
+
|
147
|
+
.comment_container.clearfix
|
148
|
+
|
149
|
+
= link_to user_path(comment.user_id),class:'comment_user_link' do
|
150
|
+
|
151
|
+
p.user_name #{comment.user.user_name}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
p.comment #{comment.comment_text}
|
156
|
+
|
157
|
+
time.comment_time = time_ago_in_words(comment.created_at) +'前'
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
- if logged_in?
|
164
|
+
|
165
|
+
.comment_form_wrapper.clearfix
|
166
|
+
|
167
|
+
p#js_image_error.input_error
|
168
|
+
|
169
|
+
= form_for(@comment,data:{ remote: true}) do |f|
|
170
|
+
|
171
|
+
= f.hidden_field :user_id,value: current_user.id
|
172
|
+
|
173
|
+
= f.hidden_field :post_id,value: @post.id
|
174
|
+
|
175
|
+
= f.text_field :comment_text,placeholder: 'コメントする',id: 'comment',class: 'comment_form'
|
176
|
+
|
177
|
+
= f.submit '投稿',id: 'post_comment', class: 'comment_form_btn',disable_with: '投稿しています。'
|
178
|
+
|
179
|
+
- else
|
180
|
+
|
181
|
+
.recommend_login_box
|
182
|
+
|
183
|
+
p.recommend_login 新規登録/ログインしてコメントする
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
`comments/create`controller
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
|
195
|
+
def create
|
196
|
+
|
197
|
+
@comment = current_user.comments.build(comment_params)
|
198
|
+
|
199
|
+
if @comment.save
|
200
|
+
|
201
|
+
@comments_count = update_comments_count(@comment.post_id)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
# render json: @comment
|
206
|
+
|
207
|
+
# render
|
208
|
+
|
209
|
+
else
|
210
|
+
|
211
|
+
flash.now[:danger] = 'コメントに失敗しました。再度お試しください。'
|
212
|
+
|
213
|
+
render 'posts/show'
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
`comments/create`のレスポンスを受け取る`views/comments/`にあるcreate.js.slimファイル。
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
```javascript
|
228
|
+
|
229
|
+
// ajaxのレスポンスが帰ってきたら、コメントの値をフォームから削除
|
230
|
+
|
231
|
+
$('#comment').val('');
|
232
|
+
|
233
|
+
// コメント数をカウントアップ
|
234
|
+
|
235
|
+
$('#comment_count_num').html(@comments_count);
|
236
|
+
|
237
|
+
// コメント内容を追加表示
|
238
|
+
|
239
|
+
$('.comment_wrapper').append("#{ j(render "shared/comment")}").hide().fadeIn('slow');
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
`views/shared/`にある_comment.html.slimファイル。
|
246
|
+
|
247
|
+
create.js.slimから呼び出したい・・・。
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
```html
|
252
|
+
|
253
|
+
.comment_container.clearfix
|
254
|
+
|
255
|
+
= link_to user_path(@comment.user_id),class:'comment_user_link' do
|
256
|
+
|
257
|
+
p.user_name #{@comment.user.user_name}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
p.comment #{@comment.comment_text}
|
262
|
+
|
263
|
+
time.comment_time = time_ago_in_words(@comment.created_at) +'前'
|
264
|
+
|
265
|
+
```
|