質問編集履歴

2

改善

2020/05/12 13:57

投稿

terusia
terusia

スコア10

test CHANGED
File without changes
test CHANGED
@@ -10,111 +10,195 @@
10
10
 
11
11
  ### 発生している問題・エラーメッセージ
12
12
 
13
-
14
-
15
- NameError in TweetsController#destroy
16
-
17
- uninitialized constant Tweet::Image
13
+ ![イメージ説明](dcf60d12a50ce464ddb381a97b62899c.png)
14
+
18
-
15
+ ### 該当のソースコード
16
+
17
+
18
+
19
- ```
19
+ ```
20
+
21
+ #tweet.rb モデル
22
+
23
+
24
+
25
+ belongs_to :user
26
+
27
+ has_many :comments, dependent: :destroy
28
+
29
+ has_many :images, dependent: :destroy
30
+
31
+ belongs_to_active_hash :category
32
+
33
+ mount_uploader :image, ImageUploader
34
+
35
+
36
+
37
+ ```
38
+
39
+
40
+
41
+ ```
42
+
43
+ #マイグレーションファイル
44
+
45
+ ~省略~
46
+
47
+ create_table :tweets do |t|
48
+
49
+ t.string :title, null: false
50
+
51
+ t.string :text
52
+
53
+ t.string :image, null: false
54
+
55
+ t.integer :user_id
56
+
57
+ t.integer :category_id, null: false
58
+
59
+ t.timestamps
60
+
61
+ ~省略~
62
+
63
+ ```
64
+
65
+
66
+
67
+ ```ruby
68
+
69
+ #tweets_controller.rb コントローラー
70
+
71
+ ~省略~
72
+
73
+ def destroy
74
+
75
+ tweet = Tweet.find(params[:id])
76
+
77
+ tweet.destroy
78
+
79
+ redirect_to action: 'index'
80
+
81
+ end
82
+
83
+
84
+
85
+ ~省略~
86
+
87
+ def tweet_params
88
+
89
+ params.require(:tweet).permit(:title, :category_id, :image, :text).merge(user_id: current_user.id)
90
+
91
+ end
92
+
93
+
94
+
95
+ ```
96
+
97
+
98
+
99
+ ```HTML
100
+
101
+ <div class="row">
102
+
103
+ <div class="container">
104
+
105
+ <%= form_with(model: @tweet, local: true) do |f| %>
106
+
107
+ <h3>投稿する</h3>
108
+
109
+ <%= render 'shared/error_messages', model: f.object %>
110
+
111
+ <%= f.text_field :title, placeholder: "タイトル" %>
112
+
113
+ <%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "選択してください" } %>
114
+
115
+ <%= f.file_field :image %> #画像フォーム
116
+
117
+ <%= f.text_area :text, placeholder: "コメント" , rows: "10" %>
118
+
119
+ <%= f.submit "SEND" %>
120
+
121
+ <% end %>
122
+
123
+ </div>
124
+
125
+ </div>
126
+
127
+ ```
128
+
129
+
130
+
131
+ ### 試したこと
132
+
133
+ CarrierWaveを使って画像などを保存しているのでremove_image!を追加して保存されたものを消そうとしたが同じエラーが出た
134
+
135
+
136
+
137
+ ```ruby
138
+
139
+ def destroy
140
+
141
+ tweet = Tweet.find(params[:id])
142
+
143
+ tweet.remove_image!
144
+
145
+ tweet.save
146
+
147
+ tweet.destroy
148
+
149
+ redirect_to action: 'index'
150
+
151
+ end
152
+
153
+ ```
154
+
155
+ 投稿のユーザーIDとログインしているユーザーIDが同じであれば消せるようにしたがuser_idがnil:classとエラーが出て削除できなかった
156
+
157
+ SequelProの保存データを見るとtweetsテーブルにuser_idがありちゃんと保存されていた
158
+
159
+ ```ruby
160
+
161
+ def destroy
162
+
163
+ if @tweet.user_id == current_user.id && @tweet.destroy
164
+
165
+ redirect_to action: 'index'
166
+
167
+ else
168
+
169
+ render :show
170
+
171
+ end
172
+
173
+ end
174
+
175
+ ```
176
+
177
+ 上記コードでの
178
+
179
+ ![イメージ説明](88ddba6872c041474f2f6d2fd27f5350.png)
180
+
181
+ ### 補足情報
182
+
183
+ CarrierWaveを使わずにネットにある画像urlを使い、画像などを保存していた時にはこちらのコードで削除できていた
184
+
185
+ ```ruby
20
186
 
21
187
  def destroy
22
188
 
23
189
    tweet = Tweet.find(params[:id])
24
190
 
25
-   tweet.destroy #ここが赤く表示
191
+   tweet.destroy
26
192
 
27
193
  redirect_to action: 'index'
28
194
 
29
- end
195
+ end
30
-
196
+
31
- ```
197
+ ```
32
-
33
- ### 該当のソースコード
198
+
34
-
35
-
36
-
37
- ```
38
-
39
- #tweet.rb モデル
40
-
41
-
42
-
43
- belongs_to :user
44
-
45
- has_many :comments, dependent: :destroy
199
+ ※HTMLの記載はhamlを変換ツールで変換したものなので誤字脱字の可能性あり
46
-
47
- has_many :images, dependent: :destroy
200
+
48
-
49
- belongs_to_active_hash :category
50
-
51
- mount_uploader :image, ImageUploader
52
-
53
-
54
-
55
- ```
56
-
57
-
58
-
59
- ```
60
-
61
- #マイグレーションファイル
62
-
63
- ~省略~
64
-
65
- create_table :tweets do |t|
66
-
67
- t.string :title, null: false
68
-
69
- t.string :text
70
-
71
- t.string :image, null: false
72
-
73
- t.integer :user_id
74
-
75
- t.integer :category_id, null: false
76
-
77
- t.timestamps
78
-
79
- ~省略~
80
-
81
- ```
82
-
83
-
84
-
85
- ```ruby
201
+ ```html
86
-
87
- #tweets_controller.rb コントローラー
88
-
89
- ~省略~
90
-
91
- def destroy
92
-
93
- tweet = Tweet.find(params[:id])
94
-
95
- tweet.destroy
96
-
97
- redirect_to action: 'index'
98
-
99
- end
100
-
101
-
102
-
103
- ~省略~
104
-
105
- def tweet_params
106
-
107
- params.require(:tweet).permit(:title, :category_id, :image, :text).merge(user_id: current_user.id)
108
-
109
- end
110
-
111
-
112
-
113
- ```
114
-
115
-
116
-
117
- ```HTML
118
202
 
119
203
  <div class="row">
120
204
 
@@ -130,7 +214,7 @@
130
214
 
131
215
  <%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "選択してください" } %>
132
216
 
133
- <%= f.file_field :image %> #画像フォーム
217
+ <%= f.text_field :image, placeholder: "投稿画像" %>
134
218
 
135
219
  <%= f.text_area :text, placeholder: "コメント" , rows: "10" %>
136
220
 
@@ -140,104 +224,6 @@
140
224
 
141
225
  </div>
142
226
 
143
- </div>
144
-
145
- ```
146
-
147
-
148
-
149
- ### 試したこと
150
-
151
- CarrierWaveを使って画像などを保存しているのでremove_image!を追加して保存されたものを消そうとしたが同じエラーが出た
152
-
153
-
154
-
155
- ```ruby
156
-
157
- def destroy
158
-
159
- tweet = Tweet.find(params[:id])
160
-
161
- tweet.remove_image!
162
-
163
- tweet.save
164
-
165
- tweet.destroy
166
-
167
- redirect_to action: 'index'
168
-
169
- end
170
-
171
- ```
172
-
173
- 投稿のユーザーIDとログインしているユーザーIDが同じであれば消せるようにしたがuser_idがnil:classとエラーが出て削除できなかった
174
-
175
- SequelProの保存データを見るとtweetsテーブルにuser_idがありちゃんと保存されていた
176
-
177
- ```ruby
178
-
179
- def destroy
180
-
181
- if @tweet.user_id == current_user.id && @tweet.destroy
182
-
183
- redirect_to action: 'index'
184
-
185
- else
186
-
187
- render :show
188
-
189
- end
190
-
191
- end
192
-
193
- ```
194
-
195
- ### 補足情報
196
-
197
- CarrierWaveを使わずにネットにある画像urlを使い、画像などを保存していた時にはこちらのコードで削除できていた
198
-
199
- ```ruby
200
-
201
- def destroy
202
-
203
-   tweet = Tweet.find(params[:id])
204
-
205
-   tweet.destroy
206
-
207
- redirect_to action: 'index'
208
-
209
- end
210
-
211
- ```
212
-
213
- ※HTMLの記載はhamlを変換ツールで変換したものなので誤字脱字の可能性あり
214
-
215
- ```html
216
-
217
- <div class="row">
218
-
219
- <div class="container">
220
-
221
- <%= form_with(model: @tweet, local: true) do |f| %>
222
-
223
- <h3>投稿する</h3>
224
-
225
- <%= render 'shared/error_messages', model: f.object %>
226
-
227
- <%= f.text_field :title, placeholder: "タイトル" %>
228
-
229
- <%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "選択してください" } %>
230
-
231
- <%= f.text_field :image, placeholder: "投稿画像" %>
232
-
233
- <%= f.text_area :text, placeholder: "コメント" , rows: "10" %>
234
-
235
- <%= f.submit "SEND" %>
236
-
237
- <% end %>
238
-
239
- </div>
240
-
241
227
  ```
242
228
 
243
229
  SequelProでのローカル環境の保存データ

1

改善

2020/05/12 13:57

投稿

terusia
terusia

スコア10

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  保存したデータを削除した
6
6
 
7
- ネットにある画像urlを使い保存していた時には削除できていたが、CarrierWaveを使用して画像を保存するように変えたら保存したデータを削除することができなくなった
7
+ ネットにある画像urlを使い保存していた時には削除できていたが、CarrierWaveを導入して画像を保存するように変えたら保存したデータを削除することができなくなった
8
8
 
9
9
 
10
10