質問編集履歴

1

内容の変更

2021/04/15 08:58

投稿

popi06
popi06

スコア3

test CHANGED
@@ -1 +1 @@
1
- rails グループ機能の不明点ついて教えていただきたいで
1
+ rails グループ機能の管理者を予めグループユーザーにする方法
test CHANGED
@@ -1,685 +1,219 @@
1
- railsでユーザーが自由にグループに入ることができるグループ機能を制作しているのですが、他の機能は自分なりになんとか制作することができるのですが、いいちはっきりしない、またはどのように実装れば良いかわからない点が2点あるので質問させていただきました
1
+ railsでユーザーが自由にグループに入ることができるグループ機能を制作しています。
2
+
3
+
4
+
2
-
5
+ 1グループ(circle)を作成したら、作成したユーザーがオーナーになる。
6
+
7
+ 2グループオーナーは予めグループユーザーである 
8
+
9
+
10
+
11
+ 2番目のグループオーナーを予めグループユーザーにする方法がわかりません。
12
+
3
- 初学者なのでわかりやすく教えていただくとありがたいです。
13
+ 初学者なのでわかりやすく教えていただたいです。
4
-
5
-
6
-
7
- 1 グループのオーナー(@circle.owner)がグループを作ると同時にグループユーザーになる。
14
+
8
-
9
- 2 参加ボタンを押してもグループユーザーにならないこと
15
+
10
-
11
-
12
-
13
- そもそもグループ参加機能、グループユーザーが実装できていない気がしたので質問させていただきました。
14
-
15
- どちらでも良いので、教えていただくとありがたいです。
16
16
 
17
17
 
18
18
 
19
19
  ```ここに言語を入力
20
20
 
21
- routes.rb
22
-
23
-
24
-
25
- Rails.application.routes.draw do
26
-
27
- root 'static_pages#home'
28
-
29
- get 'items/new'
30
-
31
- get 'password_resets/new'
32
-
33
- get 'password_resets/edit'
34
-
35
- get '/member', to: 'circles#member'
36
-
37
- get '/about', to: 'static_pages#about'
38
-
39
- get '/contact', to: 'static_pages#contact'
40
-
41
- get '/signup', to: 'users#new'
42
-
43
- get '/add', to: 'circles#new'
44
-
45
- get '/login', to: 'sessions#new'
46
-
47
- post '/login', to: 'sessions#create'
48
-
49
-
50
-
51
- delete '/logout', to: 'sessions#destroy'
52
-
53
- resources :users do
54
-
55
- member do
56
-
57
- get :following, :followers
58
-
59
- end
60
-
61
- end
62
-
63
- # デバイス
64
-
65
- devise_for :users, controllers: {
66
-
67
- registrations: 'users/registrations',
68
-
69
- passwords: 'users/passwords'
70
-
71
- }
72
-
73
- devise_scope :user do
74
-
75
- post 'users/guest_sign_in', to: 'users/sessions#new_guest'
76
-
77
- end
78
-
79
-
80
-
81
- # ユーザー
82
-
83
- resources :users
84
-
85
- resources :account_activations, only:[:edit]
86
-
87
- resources :password_resets, only: [:new, :create, :edit, :update]
88
-
89
- resources :microposts, only: [:create, :destroy]
90
-
91
- # サークル
92
-
93
- resources :circles do
94
-
95
- resources :applies, only:[:index, :create, :destroy]
96
-
97
- resources :circle_users
98
-
99
- get :search, on: :collection
100
-
101
- ・・・
21
+ /app/controllers/circles_controller.rb
22
+
23
+
24
+
25
+ class CirclesController < ApplicationController
26
+
27
+ before_action :set_circle, only: [:update, :destroy, :edit]
28
+
29
+ before_action :set_parents, only: [:new, :create, :destroy]
30
+
31
+ before_action :owner_user, only: [:edit, :destroy]
32
+
33
+
34
+
35
+ def index
36
+
37
+ @circles = Circle.paginate(page: params[:page]).search(params[:search])
38
+
39
+ end
40
+
41
+
42
+
43
+ def member
44
+
45
+ @circle_user = CircleUser.paginate(page: params[:page])
46
+
47
+ end
48
+
49
+
50
+
51
+ def new
52
+
53
+ @circle = Circle.new
54
+
55
+ if !@circle.user.include?(current_user)
56
+
57
+ @circle.user << current_user
58
+
59
+ end
60
+
61
+ end
62
+
63
+
64
+
65
+ def show
66
+
67
+ @circle = Circle.find(params[:id])
68
+
69
+ @user = User.find(params[:id])
70
+
71
+ @circleposts = Circlepost.where(circle_id: @circle.id).all
72
+
73
+ end
74
+
75
+
76
+
77
+ def create
78
+
79
+ @circle = Circle.new(circle_params)
80
+
81
+ @circle.owner = current_user ※一番目の処理
82
+
83
+ if @circle.save
84
+
85
+ @circle_user = @circle.owner
86
+
87
+ flash[:success] = "作成しました"
88
+
89
+ redirect_to root_url
90
+
91
+ else
92
+
93
+ render :new
94
+
95
+ end
96
+
97
+ end
98
+
99
+
100
+
101
+ def edit
102
+
103
+ @circle = Circle.find(params[:id])
104
+
105
+ end
106
+
107
+
108
+
109
+ def update
110
+
111
+ @circle = Circle.find(params[:id])
112
+
113
+ if @circle.update(circle_params)
114
+
115
+ flash[:success] = "サークル情報が更新されました"
116
+
117
+ redirect_to @circle
118
+
119
+ else
120
+
121
+ render 'edit'
122
+
123
+ end
124
+
125
+ end
126
+
127
+
128
+
129
+ def destroy
130
+
131
+ @circle = Circle.find_by(id: params[:id])
132
+
133
+ return redirect_to :root if @circle.nil?
134
+
135
+ @circle.destroy
136
+
137
+ flash[:success] = 'サークルを削除しました。'
138
+
139
+ redirect_to :root
140
+
141
+ end
142
+
143
+
144
+
145
+ def search
146
+
147
+ if params[:name].present?
148
+
149
+ @circles = Circle.where('name LIKE ?', "%#{params[:name]}%")
150
+
151
+ else
152
+
153
+ @circles = Circle.none
154
+
155
+ end
156
+
157
+ end
158
+
159
+
160
+
161
+ def set_parents
162
+
163
+ @parents = Category.where(ancestry: nil)
164
+
165
+ end
166
+
167
+
168
+
169
+ def get_category_children
170
+
171
+ @category_children = Category.find("#{params[:parent_id]}").children
172
+
173
+ end
174
+
175
+
176
+
177
+ def get_category_grandchildren
178
+
179
+ @category_grandchildren = Category.find("#{params[:child_id]}").children
180
+
181
+ end
182
+
183
+
184
+
185
+ ・・・
186
+
187
+ private
188
+
189
+
190
+
191
+ def owner_user
192
+
193
+ redirect_to(root_url) unless current_user && @circle.owner?
194
+
195
+ end
196
+
197
+
198
+
199
+ def circle_params
200
+
201
+ params.require(:circle).permit(:categries_id, :category_grandchildren, :category_children, :parent_id, :name, :place, :time, :homepage, :profile, { :user_ids => [] })
202
+
203
+ end
204
+
205
+
206
+
207
+ def set_circle
208
+
209
+ @circle = Circle.find(params[:id])
210
+
211
+ end
212
+
213
+
214
+
215
+ end
216
+
217
+
102
218
 
103
219
  ```
104
-
105
- ```ここに言語を入力
106
-
107
- /app/controllers/circles_controller.rb
108
-
109
-
110
-
111
- class CirclesController < ApplicationController
112
-
113
- before_action :set_circle, only: [:update, :destroy, :edit]
114
-
115
- before_action :set_parents, only: [:new, :create, :destroy]
116
-
117
- before_action :owner_user, only: [:edit, :destroy]
118
-
119
-
120
-
121
- def index
122
-
123
- @circles = Circle.paginate(page: params[:page]).search(params[:search])
124
-
125
- end
126
-
127
-
128
-
129
- def member
130
-
131
- @circle_user = CircleUser.paginate(page: params[:page])
132
-
133
- end
134
-
135
-
136
-
137
- def new
138
-
139
- @circle = Circle.new
140
-
141
- if !@circle.user.include?(current_user)
142
-
143
- @circle.user << current_user
144
-
145
- end
146
-
147
- end
148
-
149
-
150
-
151
- def show
152
-
153
- @circle = Circle.find(params[:id])
154
-
155
- @user = User.find(params[:id])
156
-
157
- @circleposts = Circlepost.where(circle_id: @circle.id).all
158
-
159
- end
160
-
161
-
162
-
163
- def create
164
-
165
- @circle = Circle.new(circle_params)
166
-
167
- @circle.owner = current_user
168
-
169
- # @circle_user.id = current_user.id
170
-
171
- if @circle.save
172
-
173
- @circle_user = @circle.owner
174
-
175
- flash[:success] = "作成しました"
176
-
177
- redirect_to root_url
178
-
179
- else
180
-
181
- render :new
182
-
183
- end
184
-
185
- end
186
-
187
-
188
-
189
- def edit
190
-
191
- @circle = Circle.find(params[:id])
192
-
193
- end
194
-
195
-
196
-
197
- def update
198
-
199
- @circle = Circle.find(params[:id])
200
-
201
- if @circle.update(circle_params)
202
-
203
- flash[:success] = "サークル情報が更新されました"
204
-
205
- redirect_to @circle
206
-
207
- else
208
-
209
- render 'edit'
210
-
211
- end
212
-
213
- end
214
-
215
-
216
-
217
- def destroy
218
-
219
- @circle = Circle.find_by(id: params[:id])
220
-
221
- return redirect_to :root if @circle.nil?
222
-
223
- @circle.destroy
224
-
225
- flash[:success] = 'サークルを削除しました。'
226
-
227
- redirect_to :root
228
-
229
- end
230
-
231
-
232
-
233
- def search
234
-
235
- if params[:name].present?
236
-
237
- @circles = Circle.where('name LIKE ?', "%#{params[:name]}%")
238
-
239
- else
240
-
241
- @circles = Circle.none
242
-
243
- end
244
-
245
- end
246
-
247
-
248
-
249
- def set_parents
250
-
251
- @parents = Category.where(ancestry: nil)
252
-
253
- end
254
-
255
-
256
-
257
- def get_category_children
258
-
259
- @category_children = Category.find("#{params[:parent_id]}").children
260
-
261
- end
262
-
263
-
264
-
265
- def get_category_grandchildren
266
-
267
- @category_grandchildren = Category.find("#{params[:child_id]}").children
268
-
269
- end
270
-
271
-
272
-
273
- ・・・
274
-
275
- private
276
-
277
-
278
-
279
- def owner_user
280
-
281
- redirect_to(root_url) unless current_user && @circle.owner?
282
-
283
- end
284
-
285
-
286
-
287
- def circle_params
288
-
289
- params.require(:circle).permit(:categries_id, :category_grandchildren, :category_children, :parent_id, :name, :place, :time, :homepage, :profile, { :user_ids => [] })
290
-
291
- end
292
-
293
-
294
-
295
- def set_circle
296
-
297
- @circle = Circle.find(params[:id])
298
-
299
- end
300
-
301
-
302
-
303
- end
304
-
305
-
306
-
307
- ```
308
-
309
- ### 2 参加ボタンを押してもグループユーザーにならないこと
310
-
311
- ```ここに言語を入力
312
-
313
-
314
-
315
- /app/controllers/circle_users_controller.rb
316
-
317
-
318
-
319
- class CircleUsersController < ApplicationController
320
-
321
-
322
-
323
- def index
324
-
325
- @cicle_users = CircleUser.all
326
-
327
- end
328
-
329
-
330
-
331
- def create
332
-
333
- @circle_user = CircleUser.create(circle_id: circle_user_params[:circle_id], user_id: circle_user_params[:user_id])
334
-
335
- Apply.find(circle_user_params[:apply_id]).destroy!
336
-
337
- redirect_to circle_applies_url(@circle_user.circle)
338
-
339
- flash[:success] = "「#{@circle_user.user.name}」が、サークル:#{@circle_user.circle.name}へ加入しました。"
340
-
341
- end
342
-
343
-
344
-
345
-
346
-
347
- def destroy
348
-
349
- @circle_user = CircleUser.find(params[:id])
350
-
351
- @circle_user.destroy!
352
-
353
- @circle = Circle.find(params[:circle_id])
354
-
355
- redirect_to circle_url(@circle), notice: "サークル「#{@circle.name}」を退会しました。"
356
-
357
- end
358
-
359
-
360
-
361
- def show
362
-
363
- @circle_user = CircleUser.find(params[:id])
364
-
365
- end
366
-
367
-
368
-
369
- private
370
-
371
-
372
-
373
- def circle_user_params
374
-
375
- params.permit(:circle_id, :user_id, :apply_id)
376
-
377
- end
378
-
379
-
380
-
381
- end
382
-
383
-
384
-
385
- ```
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
- ```ここに言語を入力
394
-
395
- /app/views/applies/index.html.erb
396
-
397
-
398
-
399
- <div class="container applicant-wrapper">
400
-
401
- <h3>承認待ちユーザ一覧</h3>
402
-
403
- ・・・
404
-
405
-
406
-
407
- <%= link_to "承認", circle_circle_users_path(app.circle, user_id: app.user.id, apply_id: app.id), method: :post, class:"mini-green-link-btn font-bold text-line-none" %>
408
-
409
- <%= link_to "却下", circle_apply_path(app.circle, app), method: :delete, class:"mini-red-link-btn font-bold text-line-none" %>
410
-
411
- <br>
412
-
413
- </div>
414
-
415
- <% end %>
416
-
417
- </div>
418
-
419
- </div>
420
-
421
- ```
422
-
423
-
424
-
425
- ```ここに言語を入力
426
-
427
- /app/controllers/applies_controller.rb
428
-
429
-
430
-
431
- class AppliesController < ApplicationController
432
-
433
-
434
-
435
-
436
-
437
- def create
438
-
439
- current_user.applies.create(circle_id: apply_params[:circle_id])
440
-
441
- redirect_to circle_url(apply_params[:circle_id])
442
-
443
- flash[:success] = "加入申請しました"
444
-
445
- end
446
-
447
-
448
-
449
- def destroy
450
-
451
- @apply = Apply.find(params[:id])
452
-
453
- @apply.destroy!
454
-
455
- @circle = Circle.find(params[:circle_id])
456
-
457
- redirect_to circle_url(@circle)
458
-
459
- flash[:info] = "加入申請を取り消しました"
460
-
461
- end
462
-
463
-
464
-
465
- def index
466
-
467
- @applies = Apply.where(circle_id: params[:circle_id])
468
-
469
- end
470
-
471
-
472
-
473
- def show
474
-
475
-
476
-
477
- end
478
-
479
-
480
-
481
- private
482
-
483
-
484
-
485
- def apply_params
486
-
487
- params.permit(:circle_id)
488
-
489
- end
490
-
491
-
492
-
493
-
494
-
495
- end
496
-
497
- ```
498
-
499
-
500
-
501
- ```ここに言語を入力
502
-
503
-
504
-
505
- /app/views/circles/show.html.erb
506
-
507
-
508
-
509
- <% provide(:title, @circle.name) %>
510
-
511
-
512
-
513
- <div class="circle-profile">
514
-
515
- <h3 class="circle-title">サークル「<%= @circle.name %>」の情報</h3>
516
-
517
- <table class="attributes">
518
-
519
- <div class="approval">
520
-
521
- <% if @circle.owner? %>
522
-
523
- <%= link_to "承認待ち一覧", circle_applies_path(@circle), class:"btn btn-warning" %>
524
-
525
- <% end %>
526
-
527
- <%= link_to "メンバー", circle_circle_users_path(@circle), class:"btn btn-warning" %>
528
-
529
- </div>
530
-
531
- ・・・
532
-
533
- <div class="enrollment">
534
-
535
- <% if @circle_user %>
536
-
537
- <%= link_to '退会する', circle_circle_user_path(@circle, @circle_user), method: :delete, data:{ confirm: "コミュニティ「#{@circle.name}」を退会します。よろしいですか?" } ,class:"btn btn-warning btn-lg" %>
538
-
539
- <!--サークルには所属していないが、ログインはしている場合 -->
540
-
541
- <% elsif current_user %>
542
-
543
- <% if @apply %>
544
-
545
- <%= link_to '申請取消', circle_apply_path(@circle, @apply), method: :delete, class:"btn btn-warning btn-lg" %>
546
-
547
- <% else %>
548
-
549
- <%= link_to '加入申請', circle_applies_path(@circle), method: :post, class:"btn btn-warning btn-lg" %>
550
-
551
- <% end %>
552
-
553
- <% end %>
554
-
555
-
556
-
557
- <% if @circle.owner? %>
558
-
559
- <%= link_to "編集", edit_circle_path(@circle), class:"btn btn-warning btn-lg" %>
560
-
561
- <%= link_to "削除", circle_path, method: :delete, class:"btn btn-warning btn-lg",
562
-
563
- data: { confirm: "(確認)サークルが削除されます。" } %>
564
-
565
- <% end %>
566
-
567
- </div>
568
-
569
-
570
-
571
- </div>
572
-
573
-
574
-
575
- ```
576
-
577
-
578
-
579
- ### モデル
580
-
581
-
582
-
583
- ```ここに言語を入力
584
-
585
- circleモデル
586
-
587
- class Circle < ApplicationRecord
588
-
589
-
590
-
591
- has_many :circle_user, dependent: :destroy
592
-
593
- has_many :user, through: :circle_user, dependent: :destroy
594
-
595
- has_many :circlepost
596
-
597
- has_many :applies, dependent: :destroy
598
-
599
- validates :name, presence: true, uniqueness: true, length: { maximum: 30 }
600
-
601
-
602
-
603
- def self.search(search)
604
-
605
- if search
606
-
607
- where(['name LIKE ?', "%#{search}%"])
608
-
609
- else
610
-
611
- all
612
-
613
- end
614
-
615
- end
616
-
617
-
618
-
619
- def circle_user?(user)
620
-
621
- user.try(:include?, (user))
622
-
623
- end
624
-
625
- end
626
-
627
-
628
-
629
- ```
630
-
631
-
632
-
633
- ```ここに言語を入力
634
-
635
-
636
-
637
- userモデル
638
-
639
- ・・・
640
-
641
- has_many :circle_users, dependent: :destroy
642
-
643
- has_many :circles, through: :circle_users
644
-
645
- has_many :applies, dependent: :destroy
646
-
647
- ・・・
648
-
649
- ```
650
-
651
-
652
-
653
- ```ここに言語を入力
654
-
655
- circle_userモデル
656
-
657
- belongs_to :circle
658
-
659
- belongs_to :user
660
-
661
- ```
662
-
663
-
664
-
665
- ```ここに言語を入力
666
-
667
- applyモデル
668
-
669
-
670
-
671
- class Apply < ApplicationRecord
672
-
673
- belongs_to :user
674
-
675
- belongs_to :circle
676
-
677
- end
678
-
679
- ```
680
-
681
-
682
-
683
-
684
-
685
- 2番の方は加入申請を押しても永遠と加入申請ボタンのままで、承認した後も退会するボタンが出てきません。