質問するログイン新規登録

質問編集履歴

3

マイグレーションファイル。試したことの追加

2020/07/31 03:18

投稿

YaroTako
YaroTako

スコア8

title CHANGED
File without changes
body CHANGED
@@ -3,11 +3,34 @@
3
3
  - method: :delet(エラー画面の指定箇所)をコメントアウトすると、「いいねしたユーザー」が正しく表示されます。ですので、javascriptの関係のエラーかと思ったのですが、javascript_pack_tagを使用しているのと、投稿削除のdeleteメソッドは正しく作動しているので、原因がわかりませんでした。
4
4
  - すみませんがどなたか分かる方いらっしゃいましたら、ご教授お願いします。
5
5
 
6
+ # 試したこと
7
+ - 【tweets > index.heml.haml】= button_to 'いいねを取り消す', tweet_like_path(tweet), method: :deleteの部分を = button_to 'いいねを取り消す', tweet_like_path(tweet,like), method: :deleteに変更
8
+ →NAME ERROR undefined local variable or method `like'
9
+
10
+ - 【tweets_controller.rb】の
11
+ ```
12
+ def index
13
+ @tweets = Tweet.includes(:user).order("created_at DESC")
14
+ @like = Like.new
15
+ end
16
+ ```
17
+ ↓↓↓を下記に変更↓↓↓
18
+ ```
19
+ def index
20
+ @tweets = Tweet.includes(:user,:like).order("created_at DESC")
21
+ @like = Like.new
22
+ end
23
+ ```
24
+ →ActiveRecord::AssociationNotFoundErrorになってしまいました。
25
+
6
26
  # 環境
7
27
  rails 6.0.0
8
28
  "haml-rails", ">= 1.0", '<= 2.0.1'
9
29
  'pg', '>= 0.18', '< 2.0'
30
+ # 参考記事
31
+ https://qiita.com/nojinoji/items/2c66499848d882c31ffa
10
32
 
33
+
11
34
  # 関連のする箇所
12
35
  ### エラー画面
13
36
  ![イメージ説明](4066d16e37a32594be2a84bd71d0ebc4.png)
@@ -247,4 +270,19 @@
247
270
  end
248
271
  end
249
272
  end
273
+ ```
274
+
275
+ ## マイグレーションファイル
276
+ ##### 2020XXX_create_like.rb
277
+ ```
278
+ class CreateLikes < ActiveRecord::Migration[6.0]
279
+ def change
280
+ create_table :likes do |t|
281
+ t.references :user, null: false, foreign_key: true
282
+ t.references :tweet, null: false, foreign_key: true
283
+
284
+ t.timestamps
285
+ end
286
+ end
287
+ end
250
288
  ```

2

routes.rbの追加

2020/07/31 03:18

投稿

YaroTako
YaroTako

スコア8

title CHANGED
File without changes
body CHANGED
@@ -229,4 +229,22 @@
229
229
  belongs_to :Tweet
230
230
  belongs_to :tag
231
231
  end
232
+ ```
233
+
234
+ ##ルーティング
235
+ ##### routes.rb
236
+ ```ruby
237
+ Rails.application.routes.draw do
238
+ devise_for :users
239
+ root to: 'tweets#index'
240
+
241
+ resources :users, only: :show
242
+ resources :tweets do
243
+ resources :likes, only: [:create, :destroy]
244
+ resources :comments, only: :create
245
+ collection do
246
+ get 'search'
247
+ end
248
+ end
249
+ end
232
250
  ```

1

余分なコメントアウトの記述を削除

2020/07/30 23:38

投稿

YaroTako
YaroTako

スコア8

title CHANGED
File without changes
body CHANGED
@@ -103,13 +103,7 @@
103
103
  def create
104
104
  @tweet = Tweet.create(tweet_params)
105
105
  @tweet.user_id = current_user.id
106
- # if @tweet.save
106
+
107
- # tag_list = tag_params[:tag_names].split(/[[:blank:]]+/).select(&:present?)
108
- # @tweet.save_tags(tag_list)
109
- # redirect_to @tweet
110
- # else
111
- # render 'new'
112
- # end
113
107
  end
114
108
 
115
109
  def destroy