質問編集履歴
3
マイグレーションファイル。試したことの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,46 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
# 試したこと
|
12
|
+
|
13
|
+
- 【tweets > index.heml.haml】= button_to 'いいねを取り消す', tweet_like_path(tweet), method: :deleteの部分を = button_to 'いいねを取り消す', tweet_like_path(tweet,like), method: :deleteに変更
|
14
|
+
|
15
|
+
→NAME ERROR undefined local variable or method `like'
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
- 【tweets_controller.rb】の
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
def index
|
24
|
+
|
25
|
+
@tweets = Tweet.includes(:user).order("created_at DESC")
|
26
|
+
|
27
|
+
@like = Like.new
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
↓↓↓を下記に変更↓↓↓
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
def index
|
38
|
+
|
39
|
+
@tweets = Tweet.includes(:user,:like).order("created_at DESC")
|
40
|
+
|
41
|
+
@like = Like.new
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
→ActiveRecord::AssociationNotFoundErrorになってしまいました。
|
48
|
+
|
49
|
+
|
50
|
+
|
11
51
|
# 環境
|
12
52
|
|
13
53
|
rails 6.0.0
|
@@ -16,6 +56,12 @@
|
|
16
56
|
|
17
57
|
'pg', '>= 0.18', '< 2.0'
|
18
58
|
|
59
|
+
# 参考記事
|
60
|
+
|
61
|
+
https://qiita.com/nojinoji/items/2c66499848d882c31ffa
|
62
|
+
|
63
|
+
|
64
|
+
|
19
65
|
|
20
66
|
|
21
67
|
# 関連のする箇所
|
@@ -497,3 +543,33 @@
|
|
497
543
|
end
|
498
544
|
|
499
545
|
```
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
## マイグレーションファイル
|
550
|
+
|
551
|
+
##### 2020XXX_create_like.rb
|
552
|
+
|
553
|
+
```
|
554
|
+
|
555
|
+
class CreateLikes < ActiveRecord::Migration[6.0]
|
556
|
+
|
557
|
+
def change
|
558
|
+
|
559
|
+
create_table :likes do |t|
|
560
|
+
|
561
|
+
t.references :user, null: false, foreign_key: true
|
562
|
+
|
563
|
+
t.references :tweet, null: false, foreign_key: true
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
t.timestamps
|
568
|
+
|
569
|
+
end
|
570
|
+
|
571
|
+
end
|
572
|
+
|
573
|
+
end
|
574
|
+
|
575
|
+
```
|
2
routes.rbの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -461,3 +461,39 @@
|
|
461
461
|
end
|
462
462
|
|
463
463
|
```
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
##ルーティング
|
468
|
+
|
469
|
+
##### routes.rb
|
470
|
+
|
471
|
+
```ruby
|
472
|
+
|
473
|
+
Rails.application.routes.draw do
|
474
|
+
|
475
|
+
devise_for :users
|
476
|
+
|
477
|
+
root to: 'tweets#index'
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
resources :users, only: :show
|
482
|
+
|
483
|
+
resources :tweets do
|
484
|
+
|
485
|
+
resources :likes, only: [:create, :destroy]
|
486
|
+
|
487
|
+
resources :comments, only: :create
|
488
|
+
|
489
|
+
collection do
|
490
|
+
|
491
|
+
get 'search'
|
492
|
+
|
493
|
+
end
|
494
|
+
|
495
|
+
end
|
496
|
+
|
497
|
+
end
|
498
|
+
|
499
|
+
```
|
1
余分なコメントアウトの記述を削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,19 +208,7 @@
|
|
208
208
|
|
209
209
|
@tweet.user_id = current_user.id
|
210
210
|
|
211
|
-
|
211
|
+
|
212
|
-
|
213
|
-
# tag_list = tag_params[:tag_names].split(/[[:blank:]]+/).select(&:present?)
|
214
|
-
|
215
|
-
# @tweet.save_tags(tag_list)
|
216
|
-
|
217
|
-
# redirect_to @tweet
|
218
|
-
|
219
|
-
# else
|
220
|
-
|
221
|
-
# render 'new'
|
222
|
-
|
223
|
-
# end
|
224
212
|
|
225
213
|
end
|
226
214
|
|