Inline Attachment x Active Store로 Github 같은 이미지를 설치하여 업로드하는 방법
13146 단어 RailsActiveStorage
샘플 창고
ActiveStorage 설정
rails active_storage:install
rails db:migrate
InlineAttachment 설정
wget https://raw.githubusercontent.com/Rovak/InlineAttachment/master/dist/inline-attachment.js -P app/assets/javascripts/
wget https://raw.githubusercontent.com/Rovak/InlineAttachment/master/dist/jquery.inline-attachment.js -P app/assets/javascripts/
inline-attachment
textarea 응용 프로그램에서 그림을 드래그해서 업로드합니다./upload/image
입니다.나중에 controllerapp/assets/javascripts/application.js
//= require inline-attachment
//= require jquery.inline-attachment
$(function(){
$('.inline-attachment').inlineattachment({
urlText: '<img src="{filename}">',
uploadUrl: "/upload/image",
uploadFieldName: "asset[file]",
allowedTypes: ['image/jpeg', 'image/png', 'image/jpg', 'image/gif'],
extraHeaders: {"X-CSRF-Token": $("meta[name=csrf-token]").attr("content")}
});
});
jquery-rails 설정
Inline Attachment에서 jquery를 사용하기 때문에 jquery-rails를 설정했습니다
Gemfile
gem 'jquery-rails'
app/assets/javascripts/application.js//= require jquery
이미지 업로드 controller로 만들기
rails g controller upload image
app/controllers/upload_controller.rbclass UploadController < ApplicationController
def image
file = params[:asset][:file]
blob = ActiveStorage::Blob.create_after_upload!(
io: file,
filename: file.original_filename,
content_type: file.content_type
)
render json: { filename: url_for(blob) }
end
end
이미지 업로드 확인용 scaffold
rails g scaffold body:text
rails db:migrate
text_area class 건네주기
<%= form.text_area :body, class: "inline-attachment", size: "100x50" %>
eb를 수정하여 이미지 보기
app/views/posts/show.html.erb
<%== @post.body %>
이미지 제작 테스트
드래그 앤 드롭에 태그가 삽입되어 있습니다.
로그에도 insert가 실행 중입니다.
Started POST "/upload/image" for 127.0.0.1 at 2018-11-22 02:09:09 +0900
Processing by UploadController#image as */*
Parameters: {"asset"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x00007f0818760418 @tempfile=#<Tempfile:/tmp/RackMultipart20181122-19296-dyym8x.jpg>, @original_filename="image-1542820149354.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"asset[file]\"; filename=\"image-1542820149354.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Disk Storage (1.6ms) Uploaded file to key: kw5WzruEFiiEDDwkqtNXZHnK (checksum: rkmRclvUbaP9VARI0QxhAw==)
(0.1ms) begin transaction
↳ app/controllers/upload_controller.rb:4
ActiveStorage::Blob Create (0.3ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["key", "kw5WzruEFiiEDDwkqtNXZHnK"], ["filename", "image-1542820149354.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 33760], ["checksum", "rkmRclvUbaP9VARI0QxhAw=="], ["created_at", "2018-11-21 17:09:09.416249"]]
↳ app/controllers/upload_controller.rb:4
(6.3ms) commit transaction
↳ app/controllers/upload_controller.rb:4
Completed 200 OK in 51ms (Views: 0.2ms | ActiveRecord: 7.4ms)
기사를 써봤어요.
성공했어!
참고 자료
Reference
이 문제에 관하여(Inline Attachment x Active Store로 Github 같은 이미지를 설치하여 업로드하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ihatov08/items/92c14a340cbb4b8e26b8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)