質問編集履歴
2
image_uploader.rbの記述を変更。
test
CHANGED
File without changes
|
test
CHANGED
@@ -356,66 +356,120 @@
|
|
356
356
|
|
357
357
|
```rails
|
358
358
|
|
359
|
-
class Image
|
359
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
360
|
+
|
360
|
-
|
361
|
+
# Include RMagick or MiniMagick support:
|
362
|
+
|
363
|
+
# include CarrierWave::RMagick
|
364
|
+
|
365
|
+
include CarrierWave::MiniMagick
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
# Choose what kind of storage to use for this uploader:
|
370
|
+
|
371
|
+
storage :file
|
372
|
+
|
373
|
+
# storage :fog
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
# Override the directory where uploaded files will be stored.
|
378
|
+
|
379
|
+
# This is a sensible default for uploaders that are meant to be mounted:
|
380
|
+
|
361
|
-
def
|
381
|
+
def store_dir
|
362
|
-
|
363
|
-
|
382
|
+
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
@image = Image.new
|
368
|
-
|
369
|
-
@image.file = files[0]
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
respond_to do |format|
|
374
|
-
|
375
|
-
if @image.save
|
376
|
-
|
377
|
-
format.html { redirect_to @image, notice: 'Image was successfully created.' }
|
378
|
-
|
379
|
-
format.json do
|
380
|
-
|
381
|
-
render json: {
|
382
|
-
|
383
|
-
files:
|
384
|
-
|
385
|
-
[
|
386
|
-
|
387
|
-
{
|
388
|
-
|
389
|
-
url: @image.file.metadata['url']+"?id=#{@image.file.model.id}",
|
390
|
-
|
391
|
-
|
383
|
+
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
392
|
-
|
393
|
-
size: 0,
|
394
|
-
|
395
|
-
delete_url: "/images/delete",
|
396
|
-
|
397
|
-
delete_type: "DELETE"
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
]
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
end
|
406
|
-
|
407
|
-
else
|
408
|
-
|
409
|
-
format.html { render :new }
|
410
|
-
|
411
|
-
format.json { render json: @image.errors, status: :unprocessable_entity }
|
412
|
-
|
413
|
-
end
|
414
|
-
|
415
|
-
end
|
416
384
|
|
417
385
|
end
|
418
386
|
|
387
|
+
|
388
|
+
|
389
|
+
# Provide a default URL as a default if there hasn't been a file uploaded:
|
390
|
+
|
391
|
+
# def default_url(*args)
|
392
|
+
|
393
|
+
# # For Rails 3.1+ asset pipeline compatibility:
|
394
|
+
|
395
|
+
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
|
396
|
+
|
397
|
+
#
|
398
|
+
|
399
|
+
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
|
400
|
+
|
401
|
+
# end
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
# Process files as they are uploaded:
|
406
|
+
|
407
|
+
# process scale: [200, 300]
|
408
|
+
|
409
|
+
#
|
410
|
+
|
411
|
+
# def scale(width, height)
|
412
|
+
|
413
|
+
# # do something
|
414
|
+
|
415
|
+
# end
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
# Create different versions of your uploaded files:
|
420
|
+
|
421
|
+
# version :thumb do
|
422
|
+
|
423
|
+
# process resize_to_fit: [50, 50]
|
424
|
+
|
425
|
+
# end
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
# Add a white list of extensions which are allowed to be uploaded.
|
430
|
+
|
431
|
+
# For images you might use something like this:
|
432
|
+
|
433
|
+
def extension_whitelist
|
434
|
+
|
435
|
+
%w[jpg jpeg gif png]
|
436
|
+
|
437
|
+
end
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
# Override the filename of the uploaded files:
|
442
|
+
|
443
|
+
# Avoid using model.id or version_name here, see uploader/store.rb for details.
|
444
|
+
|
445
|
+
def filename
|
446
|
+
|
447
|
+
# 'something.jpg' if original_filename
|
448
|
+
|
449
|
+
"#{secure_token}.#{file.extension}" if original_filename.present?
|
450
|
+
|
451
|
+
end
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
# def download_url(filename)
|
456
|
+
|
457
|
+
# url(response_content_disposition: %Q{attachment; filename="#{filename}"})
|
458
|
+
|
459
|
+
# end
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
protected
|
464
|
+
|
465
|
+
def secure_token
|
466
|
+
|
467
|
+
var = :"@#{mounted_as}_secure_token"
|
468
|
+
|
469
|
+
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
|
470
|
+
|
471
|
+
end
|
472
|
+
|
419
473
|
end
|
420
474
|
|
421
475
|
```
|
1
image uploaderのコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,7 +42,7 @@
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
-
**▼ app/assets/javascripts/application.js**
|
45
|
+
**▼ /app/assets/javascripts/application.js**
|
46
46
|
|
47
47
|
|
48
48
|
|
@@ -88,7 +88,7 @@
|
|
88
88
|
|
89
89
|
|
90
90
|
|
91
|
-
**▼ app/assets/stylesheets/application.css**
|
91
|
+
**▼ /app/assets/stylesheets/application.css**
|
92
92
|
|
93
93
|
|
94
94
|
|
@@ -134,7 +134,7 @@
|
|
134
134
|
|
135
135
|
|
136
136
|
|
137
|
-
**▼ app/models/image.rb**
|
137
|
+
**▼ /app/models/image.rb**
|
138
138
|
|
139
139
|
|
140
140
|
|
@@ -162,7 +162,7 @@
|
|
162
162
|
|
163
163
|
|
164
164
|
|
165
|
-
**▼ app/controllers/images_controller.rb**
|
165
|
+
**▼ /app/controllers/images_controller.rb**
|
166
166
|
|
167
167
|
|
168
168
|
|
@@ -232,61 +232,195 @@
|
|
232
232
|
|
233
233
|
|
234
234
|
|
235
|
-
**▼ app/views/articles/_form.html.erb**
|
235
|
+
**▼ /app/views/articles/_form.html.erb**
|
236
|
-
|
237
|
-
|
238
|
-
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
-
```
|
239
|
+
```rails
|
240
|
+
|
241
|
+
<%= form_with(model: article, local: true, class: 'siimple-form') do |form| %>
|
242
|
+
|
243
|
+
<% if article.errors.any? %>
|
244
|
+
|
245
|
+
<div id="error_explanation">
|
246
|
+
|
247
|
+
<h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2>
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
<ul>
|
252
|
+
|
253
|
+
<% article.errors.full_messages.each do |message| %>
|
254
|
+
|
255
|
+
<li><%= message %></li>
|
256
|
+
|
257
|
+
<% end %>
|
258
|
+
|
259
|
+
</ul>
|
260
|
+
|
261
|
+
</div>
|
262
|
+
|
263
|
+
<% end %>
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
<div class="siimple-form-field">
|
268
|
+
|
269
|
+
<%= form.label :title, class: 'siimple-label' %>
|
270
|
+
|
271
|
+
<%= form.text_field :title, id: :article_title, class: 'siimple-input' %>
|
272
|
+
|
273
|
+
</div>
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
<div class="siimple-form-field">
|
278
|
+
|
279
|
+
<%= form.label :body, class: 'siimple-label' %>
|
280
|
+
|
281
|
+
<%= form.text_area :body, id: :article_body, class: 'editable siimple-textarea siimple-textarea--fluid' %>
|
282
|
+
|
283
|
+
</div>
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
<div class="siimple-form-field">
|
288
|
+
|
289
|
+
<%= form.submit class: 'siimple-btn siimple-btn--blue' %>
|
290
|
+
|
291
|
+
</div>
|
292
|
+
|
293
|
+
<% end %>
|
294
|
+
|
295
|
+
|
240
296
|
|
241
297
|
<script>
|
242
298
|
|
243
|
-
|
299
|
+
$(document).ready(function(){
|
244
|
-
|
300
|
+
|
245
|
-
|
301
|
+
var editor = new MediumEditor('.editable', {
|
246
|
-
|
302
|
+
|
247
|
-
|
303
|
+
// placeholder settings
|
248
|
-
|
304
|
+
|
249
|
-
|
305
|
+
placeholder: {
|
250
|
-
|
306
|
+
|
251
|
-
|
307
|
+
text: 'Type your story',
|
252
|
-
|
308
|
+
|
253
|
-
|
309
|
+
hideOnClick: true
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
});
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
$('.editable').mediumInsert({
|
318
|
+
|
319
|
+
editor: editor,
|
320
|
+
|
321
|
+
addons: {
|
322
|
+
|
323
|
+
preview: false,
|
324
|
+
|
325
|
+
images: {
|
326
|
+
|
327
|
+
fileUploadOptions: {
|
328
|
+
|
329
|
+
url: '/images/upload',
|
330
|
+
|
331
|
+
type: 'post',
|
332
|
+
|
333
|
+
acceptFileTypes: /(.|/)(gif|jpe?g|png)$/i
|
334
|
+
|
335
|
+
}
|
254
336
|
|
255
337
|
}
|
256
338
|
|
339
|
+
}
|
340
|
+
|
257
|
-
|
341
|
+
});
|
342
|
+
|
258
|
-
|
343
|
+
});
|
344
|
+
|
259
|
-
|
345
|
+
</script>
|
346
|
+
|
347
|
+
|
348
|
+
|
260
|
-
|
349
|
+
```
|
350
|
+
|
351
|
+
|
352
|
+
|
261
|
-
|
353
|
+
**▼ /app/uploaders/image_uploader.rb (※追記)**
|
354
|
+
|
355
|
+
|
356
|
+
|
262
|
-
|
357
|
+
```rails
|
358
|
+
|
359
|
+
class ImagesController < ApplicationController
|
360
|
+
|
361
|
+
def upload
|
362
|
+
|
363
|
+
files = params.require(:files)
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
@image = Image.new
|
368
|
+
|
369
|
+
@image.file = files[0]
|
370
|
+
|
371
|
+
|
372
|
+
|
263
|
-
|
373
|
+
respond_to do |format|
|
264
|
-
|
265
|
-
|
374
|
+
|
266
|
-
|
267
|
-
|
375
|
+
if @image.save
|
376
|
+
|
268
|
-
|
377
|
+
format.html { redirect_to @image, notice: 'Image was successfully created.' }
|
378
|
+
|
379
|
+
format.json do
|
380
|
+
|
269
|
-
|
381
|
+
render json: {
|
382
|
+
|
270
|
-
|
383
|
+
files:
|
384
|
+
|
385
|
+
[
|
386
|
+
|
387
|
+
{
|
388
|
+
|
389
|
+
url: @image.file.metadata['url']+"?id=#{@image.file.model.id}",
|
390
|
+
|
391
|
+
thumbnail_url: @image.file.metadata['url']+"?id=#{@image.file.model.id}",
|
392
|
+
|
393
|
+
size: 0,
|
394
|
+
|
271
|
-
url:
|
395
|
+
delete_url: "/images/delete",
|
272
|
-
|
396
|
+
|
273
|
-
type:
|
397
|
+
delete_type: "DELETE"
|
274
|
-
|
398
|
+
|
275
|
-
|
399
|
+
}
|
400
|
+
|
401
|
+
]
|
276
402
|
|
277
403
|
}
|
278
404
|
|
279
|
-
}
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
|
405
|
+
end
|
406
|
+
|
284
|
-
|
407
|
+
else
|
408
|
+
|
409
|
+
format.html { render :new }
|
410
|
+
|
411
|
+
format.json { render json: @image.errors, status: :unprocessable_entity }
|
412
|
+
|
285
|
-
|
413
|
+
end
|
286
|
-
|
414
|
+
|
287
|
-
|
415
|
+
end
|
416
|
+
|
288
|
-
|
417
|
+
end
|
418
|
+
|
419
|
+
end
|
420
|
+
|
289
|
-
```
|
421
|
+
```
|
422
|
+
|
423
|
+
|
290
424
|
|
291
425
|
|
292
426
|
|