質問編集履歴
2
groups_controller.rbのコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -78,6 +78,60 @@
|
|
78
78
|
|
79
79
|
|
80
80
|
|
81
|
+
**▼groups_controller.rb**
|
82
|
+
|
83
|
+
```rb
|
84
|
+
|
85
|
+
class GroupsController < ApplicationController
|
86
|
+
|
87
|
+
before_action :authenticate_user!
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def index
|
92
|
+
|
93
|
+
groups = current_user.groups.includes(:messages)
|
94
|
+
|
95
|
+
@groups = groups.joins(:messages).order("messages.created_at DESC")
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
def create
|
102
|
+
|
103
|
+
group = Group.create
|
104
|
+
|
105
|
+
group_user = GroupUser.create(group_id: group.id, user_id: current_user.id)
|
106
|
+
|
107
|
+
@user = User.find(params[:id])
|
108
|
+
|
109
|
+
group_user = GroupUser.create(group_id: group.id, user_id: @user.id)
|
110
|
+
|
111
|
+
@group_id = group_user.group_id
|
112
|
+
|
113
|
+
redirect_to group_messages_path(@group_id, user_id: @user.id)
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def group_params
|
122
|
+
|
123
|
+
params.permit(:id, user_ids: [])
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
|
81
135
|
**▼views/users/show.html.haml**
|
82
136
|
|
83
137
|
```haml
|
1
userとgroupモデルのコードと、GroupUserの中身がわかる画像を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,6 +124,62 @@
|
|
124
124
|
|
125
125
|
|
126
126
|
|
127
|
+
|
128
|
+
|
129
|
+
**▼models/user.rb**
|
130
|
+
|
131
|
+
```rb
|
132
|
+
|
133
|
+
class User < ApplicationRecord
|
134
|
+
|
135
|
+
devise :database_authenticatable, :registerable,
|
136
|
+
|
137
|
+
:recoverable, :rememberable, :validatable
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
validates :name, presence: true, length: {maximum: 10}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
has_many :group_users, dependent: :destroy
|
146
|
+
|
147
|
+
has_many :groups, through: :group_users
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
has_many :messages, dependent: :destroy
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
**▼models/group.rb**
|
164
|
+
|
165
|
+
```rb
|
166
|
+
|
167
|
+
class Group < ApplicationRecord
|
168
|
+
|
169
|
+
has_many :group_users, dependent: :destroy
|
170
|
+
|
171
|
+
has_many :users, through: :group_users
|
172
|
+
|
173
|
+
has_many :messages, dependent: :destroy
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
127
183
|
### 試したこと
|
128
184
|
|
129
185
|
|
@@ -152,4 +208,6 @@
|
|
152
208
|
|
153
209
|
|
154
210
|
|
155
|
-
|
211
|
+
**▼コンソールで`GroupUser.all`と打った結果**
|
212
|
+
|
213
|
+
![イメージ説明](08c1540afe3ca02b3780d87356b5770d.jpeg)
|