質問編集履歴

2

コード加筆

2020/03/09 13:27

投稿

jime
jime

スコア10

test CHANGED
File without changes
test CHANGED
@@ -66,7 +66,7 @@
66
66
 
67
67
  <a class="main-btn1" href="/posts/new" >新規投稿</a>
68
68
 
69
- <a class="main-btn2" href="/users/#{user.id}" >マイページ</a>
69
+ <a class="main-btn2" href="/users/<%= #{user.id} %>" >マイページ</a>
70
70
 
71
71
 
72
72
 

1

コード追記

2020/03/09 13:27

投稿

jime
jime

スコア10

test CHANGED
File without changes
test CHANGED
@@ -94,6 +94,260 @@
94
94
 
95
95
 
96
96
 
97
+ ```
98
+
99
+ **route.rb**
100
+
101
+
102
+
103
+ Rails.application.routes.draw do
104
+
105
+ post "login" => "users#login"
106
+
107
+ #OK
108
+
109
+ get "login" => "users#login_form"
110
+
111
+ #OK
112
+
113
+ post "logout" => "users#logout"
114
+
115
+ post "users/:id/update" => "users#update"
116
+
117
+ get "users/:id/edit" => "users#edit"
118
+
119
+
120
+
121
+ post "users/create" => "users#create"
122
+
123
+
124
+
125
+ #OK
126
+
127
+ get "signup" => "users#new"
128
+
129
+
130
+
131
+ #OK
132
+
133
+ get "users/:id" => "users#show"
134
+
135
+
136
+
137
+ get "posts/index" => "posts#index"
138
+
139
+
140
+
141
+ get "posts/new" => "posts#new"
142
+
143
+ get "posts/:id" => "posts#show"
144
+
145
+ post "posts/create" => "posts#create"
146
+
147
+
148
+
149
+ get "posts/:id/edit" => "posts#edit"
150
+
151
+ post "posts/:id/update" => "posts#update"
152
+
153
+ post "posts/:id/destroy" =>"posts#destroy"
154
+
155
+ get "/" => "home#top"
156
+
157
+ #OK
158
+
159
+ get "about" => "home#about"
160
+
161
+ #OK
162
+
163
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+ get 'auth/:provider/callback' => 'users#creates'
174
+
175
+ root 'home#top'
176
+
177
+
178
+
179
+
180
+
181
+ end
182
+
183
+ ```
184
+
185
+
186
+
187
+ ```
188
+
189
+ **controllers/posts_contoroller**
190
+
191
+
192
+
193
+ class PostsController < ApplicationController
194
+
195
+
196
+
197
+ before_action :authenticate_user
198
+
199
+
200
+
201
+ before_action :ensure_correct_user,{only:[:edit,:update,:destroy]}
202
+
203
+
204
+
205
+ def index
206
+
207
+ @post = Post.all.order(created_at: :desc)
208
+
209
+ end
210
+
211
+
212
+
213
+ def show
214
+
215
+ @post = Post.find_by(id: params[:id])
216
+
217
+ end
218
+
219
+
220
+
221
+
222
+
223
+ def new
224
+
225
+ @post = Post.new
226
+
227
+ end
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+ def create
238
+
239
+ @post = Post.new(
240
+
241
+ content: params[:content],
242
+
243
+ type: params[:type],
244
+
245
+ time: params[:time],
246
+
247
+ user_id: @current_user.id
248
+
249
+ )
250
+
251
+
252
+
253
+
254
+
255
+ if @post.save
256
+
257
+ redirect_to("/users/#{@current_user.id}")
258
+
259
+ else
260
+
261
+ render("posts/new")
262
+
263
+ end
264
+
265
+ end
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+ def edit
274
+
275
+ @post = Post.find_by(id: params[:id])
276
+
277
+ end
278
+
279
+
280
+
281
+ def update
282
+
283
+ @post = Post.find_by(id: params[:id])
284
+
285
+ @post.content = params[:content]
286
+
287
+ @post.content = params.permit(:content)[:content]
288
+
289
+ @post.time = params.permit(:time)[:time]
290
+
291
+ @post.type = params.permit(:type)[:type]
292
+
293
+
294
+
295
+ if @post.save
296
+
297
+ redirect_to("/users/#{@current_user.id}")
298
+
299
+
300
+
301
+ else
302
+
303
+ render("posts/edit")
304
+
305
+ end
306
+
307
+ end
308
+
309
+
310
+
311
+ def destroy
312
+
313
+ @post = Post.find_by(id: params[:id])
314
+
315
+ @post.destroy
316
+
317
+ redirect_to("/users/#{@current_user.id}")
318
+
319
+ end
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+ def ensure_correct_user
328
+
329
+ @post = Post.find_by(id: params[:id])
330
+
331
+ if @post.user_id != @current_user.id
332
+
333
+ flash[:notice]= "権限がありません"
334
+
335
+ redirect_to("/posts/index")
336
+
337
+ end
338
+
339
+ end
340
+
341
+
342
+
343
+ end
344
+
345
+
346
+
347
+ ```
348
+
349
+
350
+
97
351
  エラーの出ている18行目というのは
98
352
 
99
353
  ```