質問編集履歴

3

修正

2020/11/03 03:02

投稿

yasukun252
yasukun252

スコア34

test CHANGED
File without changes
test CHANGED
@@ -242,6 +242,116 @@
242
242
 
243
243
  <%= form_for(@user_form, as: 'form', url: @user_form.user, html: { class: 'directUpload', data: { 'form-data' => (@s3_direct_post.fields), 'url' => @s3_direct_post.url, 'host' => URI.parse(@s3_direct_post.url).host } }) do |f| %> # 修正
244
244
 
245
+
246
+
247
+ <script>
248
+
249
+ $(function() {
250
+
251
+ $('.directUpload').find("input:file").each(function(i, elem) {
252
+
253
+ var fileInput = $(elem);
254
+
255
+ var form = $(fileInput.parents('form:first'));
256
+
257
+ var submitButton = form.find('input[type="submit"]');
258
+
259
+ var progressBar = $("<div class='bar'></div>");
260
+
261
+ var barContainer = $("<div class='progress'></div>").append(progressBar);
262
+
263
+ fileInput.after(barContainer);
264
+
265
+ fileInput.fileupload({
266
+
267
+ fileInput: fileInput,
268
+
269
+ url: form.data('url'),
270
+
271
+ type: 'POST',
272
+
273
+ autoUpload: true,
274
+
275
+ formData: form.data('form-data'),
276
+
277
+ paramName: 'file', // S3 does not like nested name fields i.e. name="user[avatar_url]"
278
+
279
+ dataType: 'XML', // S3 returns XML if success_action_status is set to 201
280
+
281
+ replaceFileInput: false,
282
+
283
+ progressall: function (e, data) {
284
+
285
+ var progress = parseInt(data.loaded / data.total * 100, 10);
286
+
287
+ progressBar.css('width', progress + '%')
288
+
289
+ },
290
+
291
+ start: function (e) {
292
+
293
+ submitButton.prop('disabled', true);
294
+
295
+
296
+
297
+ progressBar.
298
+
299
+ css('background', 'green').
300
+
301
+ css('display', 'block').
302
+
303
+ css('width', '0%').
304
+
305
+ text("Loading...");
306
+
307
+ },
308
+
309
+ done: function(e, data) {
310
+
311
+ submitButton.prop('disabled', false);
312
+
313
+ progressBar.text("Uploading done");
314
+
315
+
316
+
317
+ // extract key and generate URL from response
318
+
319
+ var key = $(data.jqXHR.responseXML).find("Key").text();
320
+
321
+ var url = '//' + form.data('host') + '/' + key;
322
+
323
+
324
+
325
+ // create hidden field
326
+
327
+ var input = $("<input />", { type:'hidden', name: fileInput.attr('name'), value: url })
328
+
329
+ form.append(input);
330
+
331
+ },
332
+
333
+ fail: function(e, data) {
334
+
335
+ submitButton.prop('disabled', false);
336
+
337
+
338
+
339
+ progressBar.
340
+
341
+ css("background", "red").
342
+
343
+ text("Failed");
344
+
345
+ }
346
+
347
+ });
348
+
349
+ });
350
+
351
+ });
352
+
353
+ </script>
354
+
245
355
  ```
246
356
 
247
357
 

2

修正

2020/11/03 03:02

投稿

yasukun252
yasukun252

スコア34

test CHANGED
File without changes
test CHANGED
@@ -304,15 +304,15 @@
304
304
 
305
305
 
306
306
 
307
- ↳ app/forms/user_frontend/user_form.rb:5
307
+ ↳ app/forms/user_form.rb:5
308
308
 
309
309
  User Update (0.2ms) UPDATE "users" SET "profile_text" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["profile_text", ""], ["updated_at", "2020-11-03 08:50:40.670585"], ["id", 1]]
310
310
 
311
- ↳ app/forms/user_frontend/user_form.rb:5
311
+ ↳ app/forms/user_form.rb:5
312
312
 
313
313
  UserAvatar Create (0.6ms) INSERT INTO "user_avatars" ("user_id", "image", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["user_id", 1], ["image", "#<ActionDispatch::Http::UploadedFile:0x00007fa188674298>"], ["created_at", "2020-11-03 08:50:40.671752"], ["updated_at", "2020-11-03 08:50:40.671752"]]
314
314
 
315
- ↳ app/forms/user_frontend/user_form.rb:5
315
+ ↳ app/forms/user_form.rb:5
316
316
 
317
317
  (0.5ms) COMMIT
318
318
 

1

修正

2020/11/03 02:35

投稿

yasukun252
yasukun252

スコア34

test CHANGED
File without changes
test CHANGED
@@ -80,7 +80,7 @@
80
80
 
81
81
  else
82
82
 
83
- @user_form = UserFrontend::UserForm.new
83
+ @user_form = UserForm.new
84
84
 
85
85
  end
86
86
 
@@ -92,7 +92,7 @@
92
92
 
93
93
  def edit
94
94
 
95
- @user_form = UserFrontend::UserForm.new(User.find(params[:id]))
95
+ @user_form = UserForm.new(User.find(params[:id]))
96
96
 
97
97
  end
98
98
 
@@ -102,7 +102,7 @@
102
102
 
103
103
  def create
104
104
 
105
- @user_form = UserFrontend::UserForm.new
105
+ @user_form = UserForm.new
106
106
 
107
107
  @user_form.assign_attributes(params[:form])
108
108
 
@@ -126,7 +126,7 @@
126
126
 
127
127
  def update
128
128
 
129
- @user_form = UserFrontend::UserForm.new(@user)
129
+ @user_form = UserForm.new(@user)
130
130
 
131
131
  @user_form.assign_attributes(params[:form])
132
132
 
@@ -164,7 +164,7 @@
164
164
 
165
165
  forms/user_form.rb
166
166
 
167
- class UserFrontend::UserForm
167
+ class UserForm
168
168
 
169
169
  include ActiveModel::Model
170
170
 
@@ -184,8 +184,6 @@
184
184
 
185
185
  @user.build_avatar unless @user.avatar.present?
186
186
 
187
- self.inputs_destroy = @user.avatar.present?
188
-
189
187
  end
190
188
 
191
189
 
@@ -204,12 +202,6 @@
204
202
 
205
203
  end
206
204
 
207
- if inputs_destroy
208
-
209
- user.avatar.mark_for_destruction
210
-
211
- end
212
-
213
205
  end
214
206
 
215
207