【问题标题】:How store an image from URL with Active Storage如何使用 Active Storage 从 URL 存储图像
【发布时间】:2018-12-08 06:18:58
【问题描述】:

我的问题是:当我使用 Active Storage 时,如何从 URL 上传图像。我在 Stackoverflow 中使用了来自其他 post 的代码,但通过了模型方法,即我需要存储在我的表中的参数。奇怪的情况是我收到下一个错误:

ActiveSupport::MessageVerifier::InvalidSignature in PostsController#update

但是当我从这个模型重新加载显示视图时,图像会显示存储并部署在我的帖子视图中。

这是我在后模型中的代码:

类 Post before_save :grab_image def 抓取图像(网址) 下载的图像=打开(网址) self.url_image.attach(io:下载的图像,文件名:“map.jpg”,content_type:“image/jpg”) 结尾 结尾

这是我在 Controller 的 Edit Action 中的代码:

定义更新 @posturl = 参数[:post][:url_image] @post.grab_image(@posturl) respond_to 做 |格式| 如果@post.update!(post_params) format.html { redirect_to @post, notice: '帖子已成功更新。' } format.json { 渲染:显示,状态::好的,位置:@post } 别的 format.html { 渲染:编辑 } format.json { 渲染 json:@post.errors,状态::unprocessable_entity } 结尾 结尾 结尾

我得到了以下链接,其中讨论了从 URL 上传图片的机会,但是,我不知道我还能做什么:

Active Storage Overview - EdgeGuides

@omnilord/rails-5-2-activestorage-mitigating-adoption-pitfalls

【问题讨论】:

    标签: ruby-on-rails image-uploading imageurl rails-activestorage ruby-on-rails-5.2


    【解决方案1】:

    在编辑时,您需要提供附加 blob 的签名 ID,以防用户未对图像进行任何更改。

    类似下面的东西应该可以工作。您需要在表单中进行此更改。

    <%= form.file_field :image_one, value: image_one.blog.signed_id %>
    

    【讨论】:

      【解决方案2】:

      无需显式输入文件名的最简单方法是:

      require 'open-uri'
      
      url = URI.parse("https://your-url.com/abc.mp3")
      filename = File.basename(url.path)
      file = URI.open
      user = User.first
      user.avatar.attach(io: file, filename: filename)
      

      这会针对该特定用户对象自动保存头像。

      如果您使用像 S3 这样的远程服务,则可以通过以下方式检索 URL:

      user.avatar.service_url
      

      【讨论】:

      • service_url 在生产中为我工作,但在开发中引发错误我在对象控制器 before_action 中添加了一个回调:set_host_for_local_storage 然后私有 ActiveStorage::Current.host = request.base_url 如果 Rails.application。 config.active_storage.service == :local 现在 service_url 在开发和生产中都可以使用
      • service_url 仅在您使用类似 S3 的服务来存储图像时才有效。您必须使用本地磁盘在开发环境中存储资产。不过,我喜欢你添加 before_action 来处理相同的解决方法:)
      【解决方案3】:

      也许这对你有用:

      file = open(url)
      user.image.attach(io: file, filename: "temp.#{file.content_type_parse.first.split("/").last}", content_type: file.content_type_parse.first)
      

      【讨论】:

      • 我将在我的代码中测试它,如果它有效,我会分享。谢谢。
      • 我找到了一种将图像文件转换为 Base64 的方法。检查此链接:webres-studio.com/es/blog/…
      • 这段代码不起作用,和页面问题没有区别
      【解决方案4】:

      这是一个例子:

      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],
      ))
      

      【讨论】:

      • 根本不适合我。我不知道您是否将此代码与 Active Storage 一起使用?...如果可能,请告诉我您的配置。
      • 带有 Ruby 2.5.1 的普通 Rails 5.2 应用程序我什至不需要在我的模型中使用 open-uri
      • 我试图删除 open-uri,因为我读到自 Rails 5 以来它已集成,但即便如此,我的代码也无法正常工作。
      • 这是我得到的错误...ActiveSupport::MessageVerifier::InvalidSignature in PostsController#update 但最奇怪的情况是如果刷新页面,图像存储在上传文件夹中。
      • 您似乎在控制器中放置了上传方法。不对,请尝试使用您的模型。
      猜你喜欢
      • 2020-09-03
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多