Wil*_*ero 5 ruby-on-rails image-uploading imageurl rails-activestorage ruby-on-rails-5.2
我的问题是:当我使用 Active Storage 时,如何从 URL 上传图像。我在 Stackoverflow 中使用了其他帖子中的代码,但通过了模型方法,即我需要存储在表中的参数。奇怪的情况是我收到下一个错误:
ActiveSupport::MessageVerifier::InvalidSignature in PostsController#update
但是当我从这个模型重新加载显示视图时,图像显示为存储和部署在我的帖子视图中。
这是我在Post 模型中的代码:
类 Post < ApplicationRecord
需要'open-uri'
has_one_attached :image_one
has_one_attached :image_two
has_one_attached :url_image
before_save :grab_image
定义抓取图像(网址)
下载图像 = 打开(网址)
self.url_image.attach(io:downloaded_image,文件名:“map.jpg”,content_type:“image/jpg”)
结尾
结尾
这是我的控制器编辑操作中的代码:
定义更新
@posturl = params[:post][:url_image]
@post.grab_image(@posturl)
response_to do |格式|
如果@post.update!(post_params)
format.html { redirect_to @post,注意:'帖子已成功更新。' }
format.json { 渲染:显示,状态::ok,位置:@post }
别的
format.html { 渲染:编辑}
format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }
结尾
结尾
结尾
我得到了以下链接,其中讨论了从 URL 上传图像的机会,但是,我不知道我还能做什么:
@omnilord/rails-5-2-activestorage-mitigating-adoption-pitfalls
这是一个例子:
file_url = image[:image_url]
download = open(file_url)
IO.copy_stream(download,
user.image.attach(
io: download,
filename: image[:name],
content_type: image[:content_type],
))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4962 次 |
| 最近记录: |