質問編集履歴
2
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -95,3 +95,203 @@
|
|
95
95
|
Rubyは2.3.1,RoRは5.0.0を使っています。
|
96
96
|
|
97
97
|
よろしくおねがいします。
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
追加情報になります
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
user.rbのコード
|
108
|
+
|
109
|
+
```
|
110
|
+
|
111
|
+
class User < ApplicationRecord
|
112
|
+
|
113
|
+
validates :name, {presence: true}
|
114
|
+
|
115
|
+
validates :email, {presence: true, uniqueness: true}
|
116
|
+
|
117
|
+
validates :password, {presence: true}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def posts
|
122
|
+
|
123
|
+
return Post.where(user_id: self.id)
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
post.rbのコード
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
class Post < ApplicationRecord
|
142
|
+
|
143
|
+
validates :content, {presence: true, length: {maximum: 140}}
|
144
|
+
|
145
|
+
validates :user_id, {presence: true}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def user
|
150
|
+
|
151
|
+
return User.find_by(id: self.user_id)
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
posts_controller.rbのコード
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
class PostsController < ApplicationController
|
170
|
+
|
171
|
+
before_action :authenticate_user
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def index
|
180
|
+
|
181
|
+
@posts = Post.all.order(created_at: :desc)
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def show
|
188
|
+
|
189
|
+
@post = Post.find_by(id: params[:id])
|
190
|
+
|
191
|
+
@user = @post.user
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
def new
|
198
|
+
|
199
|
+
@post = Post.new
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def create
|
206
|
+
|
207
|
+
@post = Post.new(
|
208
|
+
|
209
|
+
content: params[:content],
|
210
|
+
|
211
|
+
user_id: @current_user.id
|
212
|
+
|
213
|
+
)
|
214
|
+
|
215
|
+
if @post.save
|
216
|
+
|
217
|
+
flash[:notice] = "投稿を作成しました"
|
218
|
+
|
219
|
+
redirect_to("/posts/index")
|
220
|
+
|
221
|
+
else
|
222
|
+
|
223
|
+
render("posts/new")
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
def edit
|
232
|
+
|
233
|
+
@post = Post.find_by(id: params[:id])
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
def update
|
240
|
+
|
241
|
+
@post = Post.find_by(id: params[:id])
|
242
|
+
|
243
|
+
@post.content = params[:content]
|
244
|
+
|
245
|
+
if @post.save
|
246
|
+
|
247
|
+
flash[:notice] = "投稿を編集しました"
|
248
|
+
|
249
|
+
redirect_to("/posts/index")
|
250
|
+
|
251
|
+
else
|
252
|
+
|
253
|
+
render("posts/edit")
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
def destroy
|
262
|
+
|
263
|
+
@post = Post.find_by(id: params[:id])
|
264
|
+
|
265
|
+
@post.destroy
|
266
|
+
|
267
|
+
flash[:notice] = "投稿を削除しました"
|
268
|
+
|
269
|
+
redirect_to("/posts/index")
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
def ensure_correct_user
|
278
|
+
|
279
|
+
@post = Post.find_by(id: params[:id])
|
280
|
+
|
281
|
+
if @post.user_id != @current_user.id
|
282
|
+
|
283
|
+
flash[:notice] = "権限がありません"
|
284
|
+
|
285
|
+
redirect_to("/posts/index")
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
```
|
1
,,,の修正を行いました
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,13 +26,15 @@
|
|
26
26
|
|
27
27
|
<div class="post-right">
|
28
28
|
|
29
|
-
<div class="post-user-name">
|
29
|
+
<div class="post-user-name">
|
30
|
+
|
31
|
+
```
|
30
32
|
|
31
33
|
|
32
34
|
|
33
35
|
###該当のソースコード
|
34
36
|
|
35
|
-
```
|
37
|
+
```
|
36
38
|
|
37
39
|
|
38
40
|
|