質問編集履歴

2

追記

2019/12/13 12:49

投稿

yakumo02
yakumo02

スコア103

test CHANGED
File without changes
test CHANGED
@@ -147,3 +147,85 @@
147
147
  end
148
148
 
149
149
  ```
150
+
151
+
152
+
153
+ DB設計
154
+
155
+ ```
156
+
157
+ messagesテーブル
158
+
159
+
160
+
161
+ |Column|Type|Options|
162
+
163
+ |------|----|-------|
164
+
165
+ |body|text|null: false, foreign_key: true|
166
+
167
+ |image|string|null: false, foreign_key: true|
168
+
169
+ |group_id|integer|null: false, foreign_key: true|
170
+
171
+ |user_id|integer|null: false, foreign_key: true|
172
+
173
+
174
+
175
+ ### Association
176
+
177
+ - belongs_to :group
178
+
179
+ - belongs_to :user
180
+
181
+
182
+
183
+
184
+
185
+ ## usersテーブル
186
+
187
+ |Column|Type|Options|
188
+
189
+ |------|----|-------|
190
+
191
+ |email|string|null: false|
192
+
193
+ |password|string|null: false|
194
+
195
+ |nickname|string|null: false|
196
+
197
+ ### Association
198
+
199
+ - has_many :messages
200
+
201
+ - has_many :group
202
+
203
+
204
+
205
+
206
+
207
+ ## groupテーブル
208
+
209
+
210
+
211
+ |Column|Type|Options|
212
+
213
+ |------|----|-------|
214
+
215
+ |body|text|null: false, foreign_key: true|
216
+
217
+ |image|string|null: false, foreign_key: true|
218
+
219
+ |group_id|integer|null: false, foreign_key: true|
220
+
221
+ |user_id|integer|null: false, foreign_key: true|
222
+
223
+
224
+
225
+ ### Association
226
+
227
+ - has_many :messages
228
+
229
+ - has_many :user
230
+
231
+ ```

1

追記

2019/12/13 12:49

投稿

yakumo02
yakumo02

スコア103

test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,107 @@
43
43
 
44
44
 
45
45
  宜しくお願いします
46
+
47
+
48
+
49
+
50
+
51
+ 追記 コントローラー全体
52
+
53
+ ```
54
+
55
+ class GroupsController < ApplicationController
56
+
57
+ before_action :set_group, only: [:edit, :update,:destroy]
58
+
59
+ def index
60
+
61
+ end
62
+
63
+
64
+
65
+ def new
66
+
67
+ @group = Group.new
68
+
69
+ @group.users << current_user
70
+
71
+ end
72
+
73
+
74
+
75
+ def create
76
+
77
+ @group = Group.new(group_params,)
78
+
79
+ Group.create(name: group_params[:name], user_id: current_user.id)
80
+
81
+ if @group.save
82
+
83
+ redirect_to root_path, notice: 'グループを作成しました'
84
+
85
+ else
86
+
87
+ render :new
88
+
89
+ end
90
+
91
+ end
92
+
93
+
94
+
95
+ def destroy
96
+
97
+ group = Group.find(params[:id])
98
+
99
+ if group.user_id == current_user.id
100
+
101
+ group.destroy
102
+
103
+ end
104
+
105
+ end
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ def update
114
+
115
+ if @group.update(group_params)
116
+
117
+ redirect_to group_messages_path(@group), notice: 'グループを編集しました'
118
+
119
+ else
120
+
121
+ render :edit
122
+
123
+ end
124
+
125
+ end
126
+
127
+
128
+
129
+ private
130
+
131
+
132
+
133
+ def group_params
134
+
135
+ params.require(:group).permit(:name, { :user_ids => [] })
136
+
137
+ end
138
+
139
+
140
+
141
+ def set_group
142
+
143
+ @group = Group.find(params[:id])
144
+
145
+ end
146
+
147
+ end
148
+
149
+ ```