質問編集履歴

6

修正

2017/11/03 03:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -454,7 +454,7 @@
454
454
 
455
455
  == render "participates", team: @team
456
456
 
457
- - if current_user && !team.created_by?(current_user) && current_user.participating?(team)
457
+ - if current_user && current_user.participating?(team)
458
458
 
459
459
  == render "unparticipates", team: @team
460
460
 

5

修正の追加

2017/11/03 03:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -104,13 +104,21 @@
104
104
 
105
105
  def show
106
106
 
107
+ @team = Team.find(params[:id])
108
+
109
+ end
110
+
111
+
112
+
113
+ def edit
114
+
107
115
  省略
108
116
 
109
117
  end
110
118
 
111
119
 
112
120
 
113
- def edit
121
+ def update
114
122
 
115
123
  省略
116
124
 
@@ -118,7 +126,7 @@
118
126
 
119
127
 
120
128
 
121
- def update
129
+ def create
122
130
 
123
131
  省略
124
132
 
@@ -126,7 +134,7 @@
126
134
 
127
135
 
128
136
 
129
- def create
137
+ def new
130
138
 
131
139
  省略
132
140
 
@@ -134,7 +142,7 @@
134
142
 
135
143
 
136
144
 
137
- def new
145
+ def destroy
138
146
 
139
147
  省略
140
148
 
@@ -142,14 +150,6 @@
142
150
 
143
151
 
144
152
 
145
- def destroy
146
-
147
- 省略
148
-
149
- end
150
-
151
-
152
-
153
153
  # ユーザーをチームに参加させるアクションです
154
154
 
155
155
  def participate
@@ -466,4 +466,4 @@
466
466
 
467
467
  しかし、上記のteams/index.html.slimでは正しく動きません。
468
468
 
469
- 何か、アドバイスなどよろしくお願いいたします。
469
+ 何か、アドバイスなどございましたら、よろしくお願いいたします。

4

修正の追加

2017/11/03 03:44

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -370,7 +370,7 @@
370
370
 
371
371
  == render "participates", team: team
372
372
 
373
- - if current_user && !group.created_by?(current_user) && current_user.participating?(team)
373
+ - if current_user && !team.created_by?(current_user) && current_user.participating?(team)
374
374
 
375
375
  == render "unparticipates", team: team
376
376
 
@@ -454,7 +454,7 @@
454
454
 
455
455
  == render "participates", team: @team
456
456
 
457
- - if current_user && !group.created_by?(current_user) && current_user.participating?(team)
457
+ - if current_user && !team.created_by?(current_user) && current_user.participating?(team)
458
458
 
459
459
  == render "unparticipates", team: @team
460
460
 

3

追加の修正

2017/11/03 03:42

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -333,3 +333,137 @@
333
333
 
334
334
 
335
335
  アドバイスなどよろしくお願いいたします。
336
+
337
+
338
+
339
+ <追記>
340
+
341
+ 以下のように再実装したところチームの詳細ページ(show)では上手くAjaxで動くようになりました。
342
+
343
+ が、しかし上記に書いているチームの一覧ページ(index)では上手く動きません。なぜでしょうか?
344
+
345
+ ```HTML
346
+
347
+ teams/index.html.slim
348
+
349
+ h2 チーム一覧
350
+
351
+ .team-contents
352
+
353
+ - if @teams.present?
354
+
355
+ - @teams.each do |team|
356
+
357
+ article.team
358
+
359
+ h2 = team.name
360
+
361
+ .team-footer
362
+
363
+ .team-detail
364
+
365
+ # この部分テンプレートが以下のファイルです。
366
+
367
+ #team-btn
368
+
369
+ - if current_user && current_user.participatable_for?(team)
370
+
371
+ == render "participates", team: team
372
+
373
+ - if current_user && !group.created_by?(current_user) && current_user.participating?(team)
374
+
375
+ == render "unparticipates", team: team
376
+
377
+ = link_to "詳細を見る", team, class: "more”
378
+
379
+ - else
380
+
381
+ p チームがありません。
382
+
383
+ ```
384
+
385
+
386
+
387
+ ```Ruby
388
+
389
+ #teams/_participates.html.slim
390
+
391
+
392
+
393
+ = link_to "参加する", [:participate, team], method: :patch, class: "more", remote: true
394
+
395
+ ```
396
+
397
+
398
+
399
+ ```Ruby
400
+
401
+ #teams/_unparticipates.html.slim
402
+
403
+
404
+
405
+ = link_to "退会する", [:unparticipate, team], method: :patch, class: "more", remote: true
406
+
407
+ ```
408
+
409
+
410
+
411
+ ```Ruby
412
+
413
+ #teams/participate.js.erb
414
+
415
+
416
+
417
+ $("#team-btn").html("<%= escape_javascript render :partial => "teams/unparticipates" ,locals: {team: @team} %>");
418
+
419
+ ```
420
+
421
+
422
+
423
+ ```Ruby
424
+
425
+ #teams/unparticipate.js.erb
426
+
427
+
428
+
429
+ $("#team-btn").html("<%= escape_javascript render :partial => "teams/participates" ,locals: {team: @team} %>");
430
+
431
+ ```
432
+
433
+
434
+
435
+
436
+
437
+ 上記のように実装しました所、以下のshowページでは上手くAjaxが動きました。
438
+
439
+ ```HTML
440
+
441
+ teams/show.html.slim
442
+
443
+ h2 チーム詳細ページ
444
+
445
+ .team-contents
446
+
447
+ h2 = @team.name
448
+
449
+ # この部分が部分テンプレートです。
450
+
451
+ #team-btn
452
+
453
+ - if current_user && current_user.participatable_for?(team)
454
+
455
+ == render "participates", team: @team
456
+
457
+ - if current_user && !group.created_by?(current_user) && current_user.participating?(team)
458
+
459
+ == render "unparticipates", team: @team
460
+
461
+ ```
462
+
463
+
464
+
465
+
466
+
467
+ しかし、上記のteams/index.html.slimでは正しく動きません。
468
+
469
+ 何か、アドバイスなどよろしくお願いいたします。

2

2017/11/03 03:41

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -198,7 +198,9 @@
198
198
 
199
199
  def team_params
200
200
 
201
- params.require(:team).permit(:name) end
201
+ params.require(:team).permit(:name)
202
+
203
+ end
202
204
 
203
205
  ```
204
206
 

1

2017/11/02 07:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -150,164 +150,162 @@
150
150
 
151
151
 
152
152
 
153
+ # ユーザーをチームに参加させるアクションです
154
+
155
+ def participate
156
+
157
+ @team = Team.find(params[:id])
158
+
159
+ current_user.participated_teams << @team
160
+
161
+ respond_to do |format|
162
+
163
+ format.html
164
+
165
+ format.js
166
+
167
+ end
168
+
169
+ end
170
+
171
+
172
+
173
+ # ユーザーをチームから退会させるアクションです
174
+
175
+ def unparticipate
176
+
177
+ @team = Team.find(params[:id])
178
+
179
+ current_user.participated_teams.destroy(Team.find(params[:id]))
180
+
181
+ respond_to do |format|
182
+
183
+ format.html
184
+
185
+ format.js
186
+
187
+ end
188
+
189
+ end
190
+
191
+
192
+
153
193
  private
154
194
 
155
195
 
156
196
 
157
- # ストロングパラメーター
197
+ # ストロングパラメーター
158
-
198
+
159
- def team_params
199
+ def team_params
160
-
200
+
161
- params.require(:team).permit(:name)
201
+ params.require(:team).permit(:name) end
162
-
202
+
163
- end
203
+ ```
204
+
205
+
206
+
164
-
207
+ ```HTML
208
+
165
-
209
+ teams/index.html.slim
210
+
166
-
211
+ h2 チーム一覧
212
+
213
+ .team-contents
214
+
215
+ - if @teams.present?
216
+
167
- # ユーザーをチームに参加させるアクションです
217
+ - @teams.each do |team|
168
-
218
+
169
- def participate
219
+ article.team
170
-
220
+
171
- @team = Team.find(params[:id])
221
+ h2 = team.name
222
+
172
-
223
+ .team-footer
224
+
225
+ .team-detail
226
+
227
+ # この部分テンプレートが以下のファイルです。
228
+
173
- current_user.participated_teams << @team
229
+ == render "participates", team: team
230
+
174
-
231
+ = link_to "詳細を見る", team, class: "more”
232
+
233
+ - else
234
+
235
+ p チームがありません。
236
+
237
+ ```
238
+
239
+
240
+
241
+ ユーザーはグループに参加することが出来ます。退会することも出来ます。
242
+
243
+ 以下の部分テンプレートのボタンをAjaxで切り替えて更新するのが目的です。
244
+
245
+ ボタンの動きはユーザーがチームに参加してる場合は「退会する」、参加していない場合は「参加する」ボタンを切り替えて表示するというものです。
246
+
247
+
248
+
249
+ ```HTML
250
+
251
+ teams/_participates.html.slim
252
+
253
+ #team-btn
254
+
255
+ - if current_user && current_user.participatable_for?(team)
256
+
257
+ = link_to "参加する", [:participate, team], method: :patch, class: "more", remote: true
258
+
259
+ - if current_user && current_user.participating?(team)
260
+
261
+ = link_to "退会する", [:unparticipate, team], method: :patch, class: "more", remote: true
262
+
263
+ ```
264
+
265
+
266
+
267
+ ```HTML
268
+
269
+ Team/participate.js.html
270
+
271
+ $(“#team-btn”).html("<%= j(render ‘teams/participates') %>");
272
+
273
+ ```
274
+
275
+
276
+
277
+ ```Ruby
278
+
279
+ config/application.rb
280
+
281
+
282
+
283
+ # 以下を追記
284
+
285
+ config.action_view.embed_authenticity_token_in_remote_forms = true
286
+
287
+ ```
288
+
289
+
290
+
291
+ ```Ruby
292
+
293
+ config/routes.rb
294
+
295
+
296
+
297
+ resources :users
298
+
175
- respond_to do |format|
299
+ resources :teams do
176
-
300
+
177
- format.html
301
+ member do
178
-
302
+
179
- format.js
303
+ patch "participate", "unparticipate"
180
304
 
181
305
  end
182
306
 
183
307
  end
184
308
 
185
-
186
-
187
- # ユーザーをチームから退会させるアクションです
188
-
189
- def unparticipate
190
-
191
- @team = Team.find(params[:id])
192
-
193
- current_user.participated_teams.destroy(Team.find(params[:id]))
194
-
195
- respond_to do |format|
196
-
197
- format.html
198
-
199
- format.js
200
-
201
- end
202
-
203
- end
204
-
205
- ```
206
-
207
-
208
-
209
- ```HTML
210
-
211
- teams/index.html.slim
212
-
213
- h2 チーム一覧
214
-
215
- .team-contents
216
-
217
- - if @teams.present?
218
-
219
- - @teams.each do |team|
220
-
221
- article.team
222
-
223
- h2 = team.name
224
-
225
- .team-footer
226
-
227
- .team-detail
228
-
229
- # この部分テンプレートが以下のファイルです。
230
-
231
- == render "participates", team: team
232
-
233
- = link_to "詳細を見る", team, class: "more”
234
-
235
- - else
236
-
237
- p チームがありません。
238
-
239
- ```
240
-
241
-
242
-
243
- ユーザーはグループに参加することが出来ます。退会することも出来ます。
244
-
245
- 以下の部分テンプレートのボタンをAjaxで切り替えて更新するのが目的です。
246
-
247
- ボタンの動きはユーザーがチームに参加してる場合は「退会する」、参加していない場合は「参加する」ボタンを切り替えて表示するというものです。
248
-
249
-
250
-
251
- ```HTML
252
-
253
- teams/_participates.html.slim
254
-
255
- #team-btn
256
-
257
- - if current_user && current_user.participatable_for?(team)
258
-
259
- = link_to "参加する", [:participate, team], method: :patch, class: "more", remote: true
260
-
261
- - if current_user && current_user.participating?(team)
262
-
263
- = link_to "退会する", [:unparticipate, team], method: :patch, class: "more", remote: true
264
-
265
- ```
266
-
267
-
268
-
269
- ```HTML
270
-
271
- Team/participate.js.html
272
-
273
- $(“#team-btn”).html("<%= j(render ‘teams/participates') %>");
274
-
275
- ```
276
-
277
-
278
-
279
- ```Ruby
280
-
281
- config/application.rb
282
-
283
-
284
-
285
- # 以下を追記
286
-
287
- config.action_view.embed_authenticity_token_in_remote_forms = true
288
-
289
- ```
290
-
291
-
292
-
293
- ```Ruby
294
-
295
- config/routes.rb
296
-
297
-
298
-
299
- resources :users
300
-
301
- resources :teams do
302
-
303
- member do
304
-
305
- patch "participate", "unparticipate"
306
-
307
- end
308
-
309
- end
310
-
311
309
  ```
312
310
 
313
311