質問編集履歴

4

変更

2020/12/28 03:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,502 +14,4 @@
14
14
 
15
15
 
16
16
 
17
- なんとかならないでしょうか。。。。。。
17
+ なんとかならないでしょうか。。。。。。¥
18
-
19
-
20
-
21
- undefined method `user_id' for nil:NilClass
22
-
23
-
24
-
25
- エラー箇所は:unless Post.find_by(public_uid: params[:id]).user_id == current_user.id
26
-
27
-
28
-
29
- 現状のソースは下記になります。
30
-
31
- routes.rb
32
-
33
- ```ruby
34
-
35
- Rails.application.routes.draw do
36
-
37
- # パスワード確認トークン用URL:サイトURL/users/confirmation?confirmation_token=トークン
38
-
39
- devise_for :users, controllers: {
40
-
41
- :registrations => 'users/registrations',
42
-
43
- :sessions => 'users/sessions',
44
-
45
- :passwords => 'users/passwords'
46
-
47
- }
48
-
49
- root 'pages#index'
50
-
51
- # get 'posts' => 'posts#index', as: :posts
52
-
53
- # get 'posts/:id' => 'posts#show', as: :post
54
-
55
- # get 'posts/new' => 'posts#new', as: :new_post
56
-
57
- # post 'posts' => 'posts#create'
58
-
59
- # get 'posts/edit/:id' => 'posts#edit', as: :edit_post
60
-
61
- # patch 'posts/:id' => 'posts#update'
62
-
63
- # delete 'posts/:id' => 'posts#destroy'
64
-
65
- resources :posts #, only:[:index, :create, :new, :edit, :update, :destroy]
66
-
67
- get ':username' => 'pages#show', as: :pages_show
68
-
69
- # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
70
-
71
- end
72
-
73
-
74
-
75
- ```
76
-
77
-
78
-
79
- pages_controller.rb
80
-
81
- ```ruby
82
-
83
- class PagesController < ApplicationController
84
-
85
- def index
86
-
87
- end
88
-
89
-
90
-
91
- def show
92
-
93
- @user = User.find_by(username: params[:username])
94
-
95
- end
96
-
97
- end
98
-
99
- ```
100
-
101
-
102
-
103
- posts_controller.rb
104
-
105
- ```ruby
106
-
107
- class PostsController < ApplicationController
108
-
109
- # before_action :authenticate_user!
110
-
111
- before_action :sign_in_required, only: [:new, :create, :edit, :update, :destroy]
112
-
113
- before_action :baria_user, only: [:edit, :destroy, :update]
114
-
115
-
116
-
117
- def index
118
-
119
- @posts = Post.all.order(id: "DESC")
120
-
121
- end
122
-
123
-
124
-
125
- def show
126
-
127
- @post = Post.find_by(public_uid: params[:id])
128
-
129
- @user = User.find_by(id: @post.user_id)
130
-
131
- end
132
-
133
-
134
-
135
- def new
136
-
137
- @post = Post.new
138
-
139
- end
140
-
141
-
142
-
143
- def create
144
-
145
- @post = Post.new(post_params)
146
-
147
- if @post.save
148
-
149
- redirect_to post_path(@post.public_uid)
150
-
151
- else
152
-
153
- render 'new'
154
-
155
- end
156
-
157
- end
158
-
159
-
160
-
161
- def edit
162
-
163
- @post = Post.find_by(public_uid: params[:id])
164
-
165
- end
166
-
167
-
168
-
169
- def update
170
-
171
- @post = Post.find(params[:id])
172
-
173
- if @post.update(post_params)
174
-
175
- redirect_to post_path(@post.public_uid)
176
-
177
- else
178
-
179
- render 'edit'
180
-
181
- end
182
-
183
- end
184
-
185
-
186
-
187
- def destroy
188
-
189
- @post = Post.find_by(public_uid: params[:id])
190
-
191
- @post.destroy
192
-
193
- redirect_to posts_path
194
-
195
- end
196
-
197
-
198
-
199
- private
200
-
201
- # 仮に悪意のあるリクエスト(指定した以外のデータを送ってくる等)を受けた際に、.permitメソッドで許可していない項目については変更されず、データの扱いがより安全になります。
202
-
203
- def post_params
204
-
205
- params.require(:post).permit(:title, :content).merge(user_id: current_user.id)
206
-
207
- end
208
-
209
-
210
-
211
- def baria_user
212
-
213
- unless Post.find_by(public_uid: params[:id]).user_id == current_user.id
214
-
215
- flash[:notice] = "権限がありません"
216
-
217
- redirect_to posts_path
218
-
219
- end
220
-
221
- end
222
-
223
- end
224
-
225
-
226
-
227
- ```
228
-
229
-
230
-
231
- user.rb
232
-
233
- ```ruby
234
-
235
- class User < ApplicationRecord
236
-
237
- has_many :posts, dependent: :destroy
238
-
239
-
240
-
241
- # Include default devise modules. Others available are:
242
-
243
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
244
-
245
- devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable, :lockable, :timeoutable, :trackable
246
-
247
-
248
-
249
- validates :username,
250
-
251
- uniqueness: true,
252
-
253
- length: { minimum: 3, maximum: 25 }
254
-
255
-
256
-
257
- def to_param
258
-
259
- return self.username
260
-
261
- end
262
-
263
- end
264
-
265
-
266
-
267
- ```
268
-
269
-
270
-
271
- post.rb
272
-
273
- ```ruby
274
-
275
- class Post < ApplicationRecord
276
-
277
- belongs_to :users, optional: true
278
-
279
- generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(20)
280
-
281
- validates :title,
282
-
283
- presence: { message: 'は空白にできません'},
284
-
285
- length: { minimum: 5, maximum: 40 }
286
-
287
- validates :content,
288
-
289
- presence: true,
290
-
291
- length: { minimum: 120 }
292
-
293
- validates :user_id,
294
-
295
- presence: true
296
-
297
- end
298
-
299
-
300
-
301
- ```
302
-
303
-
304
-
305
- schema.rb
306
-
307
- ```ruby
308
-
309
- # This file is auto-generated from the current state of the database. Instead
310
-
311
- # of editing this file, please use the migrations feature of Active Record to
312
-
313
- # incrementally modify your database, and then regenerate this schema definition.
314
-
315
- #
316
-
317
- # This file is the source Rails uses to define your schema when running `bin/rails
318
-
319
- # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
320
-
321
- # be faster and is potentially less error prone than running all of your
322
-
323
- # migrations from scratch. Old migrations may fail to apply correctly if those
324
-
325
- # migrations use external dependencies or application code.
326
-
327
- #
328
-
329
- # It's strongly recommended that you check this file into your version control system.
330
-
331
-
332
-
333
- ActiveRecord::Schema.define(version: 2020_12_21_073534) do
334
-
335
-
336
-
337
- create_table "posts", force: :cascade do |t|
338
-
339
- t.string "title"
340
-
341
- t.text "content"
342
-
343
- t.datetime "created_at", precision: 6, null: false
344
-
345
- t.datetime "updated_at", precision: 6, null: false
346
-
347
- t.string "public_uid"
348
-
349
- t.integer "user_id"
350
-
351
- t.index ["public_uid"], name: "index_posts_on_public_uid", unique: true
352
-
353
- end
354
-
355
-
356
-
357
- create_table "users", force: :cascade do |t|
358
-
359
- t.string "email", default: "", null: false
360
-
361
- t.string "encrypted_password", default: "", null: false
362
-
363
- t.string "reset_password_token"
364
-
365
- t.datetime "reset_password_sent_at"
366
-
367
- t.datetime "remember_created_at"
368
-
369
- t.integer "sign_in_count", default: 0, null: false
370
-
371
- t.datetime "current_sign_in_at"
372
-
373
- t.datetime "last_sign_in_at"
374
-
375
- t.string "current_sign_in_ip"
376
-
377
- t.string "last_sign_in_ip"
378
-
379
- t.string "confirmation_token"
380
-
381
- t.datetime "confirmed_at"
382
-
383
- t.datetime "confirmation_sent_at"
384
-
385
- t.string "unconfirmed_email"
386
-
387
- t.integer "failed_attempts", default: 0, null: false
388
-
389
- t.string "unlock_token"
390
-
391
- t.datetime "locked_at"
392
-
393
- t.datetime "created_at", precision: 6, null: false
394
-
395
- t.datetime "updated_at", precision: 6, null: false
396
-
397
- t.string "username"
398
-
399
- t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
400
-
401
- t.index ["email"], name: "index_users_on_email", unique: true
402
-
403
- t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
404
-
405
- t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
406
-
407
- end
408
-
409
-
410
-
411
- end
412
-
413
-
414
-
415
- ```
416
-
417
-
418
-
419
- edit.html.erb
420
-
421
- ```ruby
422
-
423
- <h1>投稿編集</h1>
424
-
425
- <%= form_with(model: @post, local: true) do |form| %>
426
-
427
- <% if @post.errors.any? %>
428
-
429
- <div style="color: red;">
430
-
431
- <h2>
432
-
433
- <%= pluralize(@post.errors.count, "つのエラー") %>
434
-
435
- </h2>
436
-
437
- <ul>
438
-
439
- <% @post.errors.full_messages.each do |msg| %>
440
-
441
- <li><%= msg %></li>
442
-
443
- <% end %>
444
-
445
- </ul>
446
-
447
- </div>
448
-
449
- <% end %>
450
-
451
-
452
-
453
- <div>
454
-
455
- <%= form.label :title, "タイトル" %><br>
456
-
457
- <%= form.text_field :title %>
458
-
459
- </div>
460
-
461
- <div id='editor'>
462
-
463
- <%= form.label :content %><br>
464
-
465
- <%= form.text_area :content, 'v-model' => 'input', name: 'post[content]' %>
466
-
467
- <div v-html='input | marked'></div>
468
-
469
- </div>
470
-
471
- <div>
472
-
473
- <%= form.submit '更新する' %>
474
-
475
- </div>
476
-
477
- <% end %>
478
-
479
- <br>
480
-
481
- <%= link_to '一覧に戻る', posts_path %>
482
-
483
-
484
-
485
- <%# プレビュー出力 %>
486
-
487
- <script>
488
-
489
- window.onload = function() {
490
-
491
- new Vue({
492
-
493
- el: '#editor',
494
-
495
- data: {
496
-
497
- input: '<%== j @post.content %>',
498
-
499
- },
500
-
501
- filters: {
502
-
503
- marked: marked,
504
-
505
- },
506
-
507
- });
508
-
509
- };
510
-
511
- </script>
512
-
513
-
514
-
515
- ```

3

追記

2020/12/28 03:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -413,3 +413,103 @@
413
413
 
414
414
 
415
415
  ```
416
+
417
+
418
+
419
+ edit.html.erb
420
+
421
+ ```ruby
422
+
423
+ <h1>投稿編集</h1>
424
+
425
+ <%= form_with(model: @post, local: true) do |form| %>
426
+
427
+ <% if @post.errors.any? %>
428
+
429
+ <div style="color: red;">
430
+
431
+ <h2>
432
+
433
+ <%= pluralize(@post.errors.count, "つのエラー") %>
434
+
435
+ </h2>
436
+
437
+ <ul>
438
+
439
+ <% @post.errors.full_messages.each do |msg| %>
440
+
441
+ <li><%= msg %></li>
442
+
443
+ <% end %>
444
+
445
+ </ul>
446
+
447
+ </div>
448
+
449
+ <% end %>
450
+
451
+
452
+
453
+ <div>
454
+
455
+ <%= form.label :title, "タイトル" %><br>
456
+
457
+ <%= form.text_field :title %>
458
+
459
+ </div>
460
+
461
+ <div id='editor'>
462
+
463
+ <%= form.label :content %><br>
464
+
465
+ <%= form.text_area :content, 'v-model' => 'input', name: 'post[content]' %>
466
+
467
+ <div v-html='input | marked'></div>
468
+
469
+ </div>
470
+
471
+ <div>
472
+
473
+ <%= form.submit '更新する' %>
474
+
475
+ </div>
476
+
477
+ <% end %>
478
+
479
+ <br>
480
+
481
+ <%= link_to '一覧に戻る', posts_path %>
482
+
483
+
484
+
485
+ <%# プレビュー出力 %>
486
+
487
+ <script>
488
+
489
+ window.onload = function() {
490
+
491
+ new Vue({
492
+
493
+ el: '#editor',
494
+
495
+ data: {
496
+
497
+ input: '<%== j @post.content %>',
498
+
499
+ },
500
+
501
+ filters: {
502
+
503
+ marked: marked,
504
+
505
+ },
506
+
507
+ });
508
+
509
+ };
510
+
511
+ </script>
512
+
513
+
514
+
515
+ ```

2

追記しました

2020/12/23 00:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,79 @@
28
28
 
29
29
  現状のソースは下記になります。
30
30
 
31
-
31
+ routes.rb
32
+
33
+ ```ruby
34
+
35
+ Rails.application.routes.draw do
36
+
37
+ # パスワード確認トークン用URL:サイトURL/users/confirmation?confirmation_token=トークン
38
+
39
+ devise_for :users, controllers: {
40
+
41
+ :registrations => 'users/registrations',
42
+
43
+ :sessions => 'users/sessions',
44
+
45
+ :passwords => 'users/passwords'
46
+
47
+ }
48
+
49
+ root 'pages#index'
50
+
51
+ # get 'posts' => 'posts#index', as: :posts
52
+
53
+ # get 'posts/:id' => 'posts#show', as: :post
54
+
55
+ # get 'posts/new' => 'posts#new', as: :new_post
56
+
57
+ # post 'posts' => 'posts#create'
58
+
59
+ # get 'posts/edit/:id' => 'posts#edit', as: :edit_post
60
+
61
+ # patch 'posts/:id' => 'posts#update'
62
+
63
+ # delete 'posts/:id' => 'posts#destroy'
64
+
65
+ resources :posts #, only:[:index, :create, :new, :edit, :update, :destroy]
66
+
67
+ get ':username' => 'pages#show', as: :pages_show
68
+
69
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
70
+
71
+ end
72
+
73
+
74
+
75
+ ```
76
+
77
+
78
+
79
+ pages_controller.rb
80
+
81
+ ```ruby
82
+
83
+ class PagesController < ApplicationController
84
+
85
+ def index
86
+
87
+ end
88
+
89
+
90
+
91
+ def show
92
+
93
+ @user = User.find_by(username: params[:username])
94
+
95
+ end
96
+
97
+ end
98
+
99
+ ```
100
+
101
+
102
+
103
+ posts_controller.rb
32
104
 
33
105
  ```ruby
34
106
 
@@ -153,3 +225,191 @@
153
225
 
154
226
 
155
227
  ```
228
+
229
+
230
+
231
+ user.rb
232
+
233
+ ```ruby
234
+
235
+ class User < ApplicationRecord
236
+
237
+ has_many :posts, dependent: :destroy
238
+
239
+
240
+
241
+ # Include default devise modules. Others available are:
242
+
243
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
244
+
245
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable, :lockable, :timeoutable, :trackable
246
+
247
+
248
+
249
+ validates :username,
250
+
251
+ uniqueness: true,
252
+
253
+ length: { minimum: 3, maximum: 25 }
254
+
255
+
256
+
257
+ def to_param
258
+
259
+ return self.username
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+ ```
268
+
269
+
270
+
271
+ post.rb
272
+
273
+ ```ruby
274
+
275
+ class Post < ApplicationRecord
276
+
277
+ belongs_to :users, optional: true
278
+
279
+ generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(20)
280
+
281
+ validates :title,
282
+
283
+ presence: { message: 'は空白にできません'},
284
+
285
+ length: { minimum: 5, maximum: 40 }
286
+
287
+ validates :content,
288
+
289
+ presence: true,
290
+
291
+ length: { minimum: 120 }
292
+
293
+ validates :user_id,
294
+
295
+ presence: true
296
+
297
+ end
298
+
299
+
300
+
301
+ ```
302
+
303
+
304
+
305
+ schema.rb
306
+
307
+ ```ruby
308
+
309
+ # This file is auto-generated from the current state of the database. Instead
310
+
311
+ # of editing this file, please use the migrations feature of Active Record to
312
+
313
+ # incrementally modify your database, and then regenerate this schema definition.
314
+
315
+ #
316
+
317
+ # This file is the source Rails uses to define your schema when running `bin/rails
318
+
319
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
320
+
321
+ # be faster and is potentially less error prone than running all of your
322
+
323
+ # migrations from scratch. Old migrations may fail to apply correctly if those
324
+
325
+ # migrations use external dependencies or application code.
326
+
327
+ #
328
+
329
+ # It's strongly recommended that you check this file into your version control system.
330
+
331
+
332
+
333
+ ActiveRecord::Schema.define(version: 2020_12_21_073534) do
334
+
335
+
336
+
337
+ create_table "posts", force: :cascade do |t|
338
+
339
+ t.string "title"
340
+
341
+ t.text "content"
342
+
343
+ t.datetime "created_at", precision: 6, null: false
344
+
345
+ t.datetime "updated_at", precision: 6, null: false
346
+
347
+ t.string "public_uid"
348
+
349
+ t.integer "user_id"
350
+
351
+ t.index ["public_uid"], name: "index_posts_on_public_uid", unique: true
352
+
353
+ end
354
+
355
+
356
+
357
+ create_table "users", force: :cascade do |t|
358
+
359
+ t.string "email", default: "", null: false
360
+
361
+ t.string "encrypted_password", default: "", null: false
362
+
363
+ t.string "reset_password_token"
364
+
365
+ t.datetime "reset_password_sent_at"
366
+
367
+ t.datetime "remember_created_at"
368
+
369
+ t.integer "sign_in_count", default: 0, null: false
370
+
371
+ t.datetime "current_sign_in_at"
372
+
373
+ t.datetime "last_sign_in_at"
374
+
375
+ t.string "current_sign_in_ip"
376
+
377
+ t.string "last_sign_in_ip"
378
+
379
+ t.string "confirmation_token"
380
+
381
+ t.datetime "confirmed_at"
382
+
383
+ t.datetime "confirmation_sent_at"
384
+
385
+ t.string "unconfirmed_email"
386
+
387
+ t.integer "failed_attempts", default: 0, null: false
388
+
389
+ t.string "unlock_token"
390
+
391
+ t.datetime "locked_at"
392
+
393
+ t.datetime "created_at", precision: 6, null: false
394
+
395
+ t.datetime "updated_at", precision: 6, null: false
396
+
397
+ t.string "username"
398
+
399
+ t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
400
+
401
+ t.index ["email"], name: "index_users_on_email", unique: true
402
+
403
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
404
+
405
+ t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
406
+
407
+ end
408
+
409
+
410
+
411
+ end
412
+
413
+
414
+
415
+ ```

1

追記

2020/12/22 23:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -7,6 +7,10 @@
7
7
 
8
8
 
9
9
  現状は、他のユーザーから編集しようとすると「権限がありません」と表示されますが、投稿したユーザー本人が編集して更新ボタンを押すと、user_idがnilになり、更新できません。
10
+
11
+
12
+
13
+ user_idカラムはPostとUserに結びつけれていて、ユーザーごとの投稿一覧などは表示できています。
10
14
 
11
15
 
12
16