質問編集履歴
2
誤字を修正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -128,6 +128,8 @@
|
|
128
128
|
|
129
129
|
|
130
130
|
|
131
|
+
|
132
|
+
|
131
133
|
【ビュー】
|
132
134
|
|
133
135
|
```
|
1
文法の修正を行いました
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,14 +3,6 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
Railsで投稿(Post)に紐付く画像(Image)が投稿できるアプリケーションを作成しております。
|
6
|
-
|
7
|
-
※ImageにはCommentが紐付きます。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Post > Image > Comment にてネストの関係にあるのですが、
|
12
|
-
|
13
|
-
Imageに削除機能を設けて、削除した時にそれに紐付くCommentも削除されるようにしたいです。
|
14
6
|
|
15
7
|
|
16
8
|
|
@@ -19,6 +11,10 @@
|
|
19
11
|
|
20
12
|
|
21
13
|
### 発生している問題・エラーメッセージ
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
![イメージ説明](0df3c6b70d99aa0b6949de72cda3851f.png)
|
22
18
|
|
23
19
|
|
24
20
|
|
@@ -36,7 +32,43 @@
|
|
36
32
|
|
37
33
|
|
38
34
|
|
35
|
+
|
36
|
+
|
37
|
+
【image.rb】
|
38
|
+
|
39
|
+
```rails
|
40
|
+
|
41
|
+
class Image < ApplicationRecord
|
42
|
+
|
43
|
+
belongs_to :post
|
44
|
+
|
45
|
+
belongs_to :user
|
46
|
+
|
47
|
+
has_many :likes, dependent: :destroy
|
48
|
+
|
49
|
+
has_many :liking_users, through: :likes, source: :user
|
50
|
+
|
51
|
+
has_many :comments, dependent: :destroy
|
52
|
+
|
53
|
+
has_many :commenting_users, through: :comments, source: :user
|
54
|
+
|
55
|
+
has_many :saves, dependent: :destroy
|
56
|
+
|
57
|
+
has_many :users, through: :saves, source: :user
|
58
|
+
|
59
|
+
validates :image, presence: true, unless: :text?
|
60
|
+
|
61
|
+
mount_uploader :image, ImageUploader
|
62
|
+
|
39
|
-
|
63
|
+
end
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
【post.rb】
|
40
72
|
|
41
73
|
```rails
|
42
74
|
|
@@ -68,133 +100,27 @@
|
|
68
100
|
|
69
101
|
end
|
70
102
|
|
71
|
-
|
72
|
-
|
73
|
-
```
|
74
|
-
|
75
|
-
```rails
|
76
|
-
|
77
|
-
class Image < ApplicationRecord
|
78
|
-
|
79
|
-
belongs_to :post
|
80
|
-
|
81
|
-
belongs_to :user
|
82
|
-
|
83
|
-
belongs_to :post_top_image, dependent: :destroy
|
84
|
-
|
85
|
-
has_many :likes, dependent: :destroy
|
86
|
-
|
87
|
-
has_many :liking_users, through: :likes, source: :user
|
88
|
-
|
89
|
-
has_many :comments, dependent: :destroy
|
90
|
-
|
91
|
-
has_many :commenting_users, through: :comments, source: :user
|
92
|
-
|
93
|
-
has_many :saves, dependent: :destroy
|
94
|
-
|
95
|
-
has_many :users, through: :saves, source: :user
|
96
|
-
|
97
|
-
validates :image, presence: true, unless: :text?
|
98
|
-
|
99
|
-
mount_uploader :image, ImageUploader
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
```
|
104
|
-
|
105
|
-
```rails
|
106
|
-
|
107
|
-
class Comment < ApplicationRecord
|
108
|
-
|
109
|
-
belongs_to :post, counter_cache: :comments_count
|
110
|
-
|
111
|
-
belongs_to :image, counter_cache: :comments_count
|
112
|
-
|
113
|
-
belongs_to :user
|
114
|
-
|
115
|
-
validates :content, presence: true
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
103
|
```
|
120
104
|
|
121
105
|
|
122
|
-
|
123
|
-
ルート
|
124
|
-
|
125
|
-
```rails
|
126
|
-
|
127
|
-
Rails.application.routes.draw do
|
128
|
-
|
129
|
-
devise_for :users
|
130
|
-
|
131
|
-
root "top#index"
|
132
|
-
|
133
|
-
resources :users, only: [:show, :edit, :update]
|
134
|
-
|
135
|
-
resources :posts, only: [:index, :new, :create, :edit, :update] do
|
136
|
-
|
137
|
-
collection do
|
138
|
-
|
139
|
-
get 'likes_sort'
|
140
|
-
|
141
|
-
get 'comments_sort'
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
resources :images do
|
146
|
-
|
147
|
-
resources :comments, only: [:index, :create]
|
148
|
-
|
149
|
-
member do
|
150
|
-
|
151
|
-
post "save", to: "saves#create"
|
152
|
-
|
153
|
-
delete "unsave", to: "saves#destroy"
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
post '/like/:post_id/:image_id' => 'likes#like', as: 'like'
|
162
|
-
|
163
|
-
delete '/like/:post_id/:image_id' => 'likes#unlike', as: 'unlike'
|
164
|
-
|
165
|
-
resources :saves, only: [:index]
|
166
|
-
|
167
|
-
resources :histories, only: [:index]
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
```
|
172
106
|
|
173
107
|
|
174
108
|
|
175
109
|
コントローラー
|
176
110
|
|
177
|
-
```rails
|
178
|
-
|
179
|
-
before_action :set_post
|
180
111
|
|
181
112
|
|
113
|
+
【images_controller.rb】
|
114
|
+
|
115
|
+
```rails
|
182
116
|
|
183
117
|
def destroy
|
184
118
|
|
185
|
-
|
119
|
+
image = Image.find(params[:id])
|
186
120
|
|
187
|
-
|
121
|
+
image.destroy
|
188
122
|
|
189
123
|
redirect_to post_images_path(@post)
|
190
|
-
|
191
|
-
end
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
def set_post
|
196
|
-
|
197
|
-
@post = Post.find(params[:post_id])
|
198
124
|
|
199
125
|
end
|
200
126
|
|
@@ -202,7 +128,7 @@
|
|
202
128
|
|
203
129
|
|
204
130
|
|
205
|
-
ビュー
|
131
|
+
【ビュー】
|
206
132
|
|
207
133
|
```
|
208
134
|
|
@@ -218,12 +144,6 @@
|
|
218
144
|
|
219
145
|
|
220
146
|
|
221
|
-
|
147
|
+
コントローラーにてdestroyをdeleteに変更するとImageモデルのデータを削除することはできました。
|
222
148
|
|
223
|
-
Image
|
149
|
+
今回はImageモデルに紐づいて削除したいモデルが複数あるため、destroyで実行したいです。
|
224
|
-
|
225
|
-
@image = Image.find_by(params[:id], post_id: params[:post_id])
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
まだまだ初心者で恐縮ですがご教示いただけますと幸いです。![イメージ説明](3ac237517145cdea8cb1970bc8fc56ea.png)
|