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

質問編集履歴

2

誤字を修正しました

2020/04/02 11:07

投稿

_3443_
_3443_

スコア5

title CHANGED
File without changes
body CHANGED
@@ -63,6 +63,7 @@
63
63
  end
64
64
  ```
65
65
 
66
+
66
67
  【ビュー】
67
68
  ```
68
69
  .comment-main__menu-destroy

1

文法の修正を行いました

2020/04/02 11:07

投稿

_3443_
_3443_

スコア5

title CHANGED
File without changes
body CHANGED
@@ -1,15 +1,13 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  Railsで投稿(Post)に紐付く画像(Image)が投稿できるアプリケーションを作成しております。
4
- ※ImageにはCommentが紐付きます。
5
4
 
6
- Post > Image > Comment にてネストの関係にあるのですが、
7
- Imageに削除機能を設けて、削除した時にそれに紐付くCommentも削除されるようにしたいです。
8
-
9
5
  Imageにdestroyアクションを追加し実行しようとしたところ下記のエラーが出ました。
10
6
 
11
7
  ### 発生している問題・エラーメッセージ
12
8
 
9
+ ![イメージ説明](0df3c6b70d99aa0b6949de72cda3851f.png)
10
+
13
11
  ```
14
12
  NameError in ImagesController#destroy
15
13
  uninitialized constant Image::Safe
@@ -17,90 +15,55 @@
17
15
 
18
16
  ### 該当のソースコード
19
17
 
18
+
20
- モデル
19
+ 【image.rb】
21
20
  ```rails
22
- class Post < ApplicationRecord
21
+ class Image < ApplicationRecord
23
- belongs_to :prefecture
22
+ belongs_to :post
24
23
  belongs_to :user
25
- belongs_to :genre
26
- belongs_to :post_top_image, dependent: :destroy
27
- has_many :images
28
24
  has_many :likes, dependent: :destroy
29
25
  has_many :liking_users, through: :likes, source: :user
30
26
  has_many :comments, dependent: :destroy
31
27
  has_many :commenting_users, through: :comments, source: :user
32
- has_many :histories, dependent: :destroy
28
+ has_many :saves, dependent: :destroy
29
+ has_many :users, through: :saves, source: :user
33
- validates :title, :pronunciation_key, :prefecture_id, :address, :genre_id, presence: true
30
+ validates :image, presence: true, unless: :text?
34
31
  mount_uploader :image, ImageUploader
35
32
  end
33
+ ```
36
34
 
35
+
37
- ```
36
+ 【post.rb】
38
37
  ```rails
39
- class Image < ApplicationRecord
38
+ class Post < ApplicationRecord
40
- belongs_to :post
39
+ belongs_to :prefecture
41
40
  belongs_to :user
41
+ belongs_to :genre
42
42
  belongs_to :post_top_image, dependent: :destroy
43
+ has_many :images
43
44
  has_many :likes, dependent: :destroy
44
45
  has_many :liking_users, through: :likes, source: :user
45
46
  has_many :comments, dependent: :destroy
46
47
  has_many :commenting_users, through: :comments, source: :user
47
- has_many :saves, dependent: :destroy
48
+ has_many :histories, dependent: :destroy
48
- has_many :users, through: :saves, source: :user
49
- validates :image, presence: true, unless: :text?
49
+ validates :title, :pronunciation_key, :prefecture_id, :address, :genre_id, presence: true
50
50
  mount_uploader :image, ImageUploader
51
51
  end
52
52
  ```
53
- ```rails
54
- class Comment < ApplicationRecord
55
- belongs_to :post, counter_cache: :comments_count
56
- belongs_to :image, counter_cache: :comments_count
57
- belongs_to :user
58
- validates :content, presence: true
59
- end
60
- ```
61
53
 
62
- ルート
63
- ```rails
64
- Rails.application.routes.draw do
65
- devise_for :users
66
- root "top#index"
67
- resources :users, only: [:show, :edit, :update]
68
- resources :posts, only: [:index, :new, :create, :edit, :update] do
69
- collection do
70
- get 'likes_sort'
71
- get 'comments_sort'
72
- end
73
- resources :images do
74
- resources :comments, only: [:index, :create]
75
- member do
76
- post "save", to: "saves#create"
77
- delete "unsave", to: "saves#destroy"
78
- end
79
- end
80
- end
81
- post '/like/:post_id/:image_id' => 'likes#like', as: 'like'
82
- delete '/like/:post_id/:image_id' => 'likes#unlike', as: 'unlike'
83
- resources :saves, only: [:index]
84
- resources :histories, only: [:index]
85
- end
86
- ```
87
54
 
88
55
  コントローラー
56
+
57
+ 【images_controller.rb】
89
58
  ```rails
90
- before_action :set_post
91
-
92
59
  def destroy
93
- @image = Image.find(params[:id])
60
+ image = Image.find(params[:id])
94
- @image.destroy
61
+ image.destroy
95
62
  redirect_to post_images_path(@post)
96
63
  end
97
-
98
- def set_post
99
- @post = Post.find(params[:post_id])
100
- end
101
64
  ```
102
65
 
103
- ビュー
66
+ ビュー
104
67
  ```
105
68
  .comment-main__menu-destroy
106
69
  = link_to "削除", post_image_path(@post, @image), method: :delete, class: "btn-image-menu"
@@ -108,8 +71,5 @@
108
71
 
109
72
  ### 試したこと
110
73
 
111
- ルーをImageのdestroyのみネストさせず別に表記しみましたが、逆PostのIDが取れていないいうエラが出ました。
74
+ コンローラーにてdestroyをdelete変更するImageモデルのデタを削除することはできました。
112
- Imageのdestroyアクションにて下記の記述に変えてみましたが結果は変わりませんでした。
75
+ 今回はImageモデル紐づい削除したいモデル複数あるため、destroy実行したいです
113
- @image = Image.find_by(params[:id], post_id: params[:post_id])
114
-
115
- まだまだ初心者で恐縮ですがご教示いただけますと幸いです。![イメージ説明](3ac237517145cdea8cb1970bc8fc56ea.png)