・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction ・ Introduction of Bootstrap3 ・ Introduction of Font Awesome -Login function implementation ・ Implementation of posting function
slim:users/edit.html.slim
/Postscript
= f.file_field :profile_image, class: 'img_field', style: 'display:none;'
= attachment_image_tag @user, :profile_image, fallback: 'no-image.png', onClick: "$('.img_field').click()", class: 'center-block img-thumbnail img-responsive img_prev'
br
file_field
from display: none
and assign a class.= f.file_field :profile_image, class: 'img_field', style: 'display:none;'
(1)
is clicked, JavaScript processing is executed.onClick: "$('.img_field').click()"
appliacation.scss
//Postscript
.img_prev:hover {
cursor: pointer;
opacity: 0.7;
transform: scale(1.01, 1.01);
}
.img_prev:hover {}
cursor: pointer;
opacity: 0.7;
transform: scale(1.01, 1.01);
Terminal
$ touch app/assets/javascripts/image_preview.js
image_preview.js
$(function () {
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.img_prev').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$('.img_field').change(function () {
readURL(this);
});
});
1. The process of editing the view with (2)
.$('.img_field').change(function () {
readURL(this);
});
Recommended Posts