简体   繁体   English

Rails 5.2(升级)-Active Storage不显示图像

[英]Rails 5.2 (Upgrade) - Active Storage does not display image

So I have upgraded from rails 5.1 to 5.2 所以我已经从Rails 5.1升级到5.2

rails app:update

It all works well, and I instantly set up the active storage configuration to be used with a new section in the web application. 一切正常,我立即设置了活动存储配置,以供Web应用程序中的新部分使用。

I created the migrations: 我创建了迁移:

rails active_storage:install
rake db:migrate

I configured conf/storage.yml - production using AWS S3: 我配置了conf / storage.yml-使用AWS S3进行生产:

test: service: Disk root: <%= Rails.root.join("tmp/storage") %> 测试:服务:磁盘根:<%= Rails.root.join(“ tmp / storage”)%>

local: service: Disk root: <%= Rails.root.join("storage") %> 本地:服务:磁盘根:<%= Rails.root.join(“存储”)%>

amazon: service: S3 access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> region: eu-west-2 bucket: whatever 亚马逊:服务:S3 access_key_id:<%= Rails.application.credentials.dig(:aws,:access_key_id)%> secret_access_key:<%= Rails.application.credentials.dig(:aws,:secret_access_key)%>地区:欧盟-west-2桶:随便

Adding the required GEM to GEMFILE: 将所需的GEM添加到GEMFILE中:

gem 'aws-sdk-s3'

Making sure that Dev environment uses the local setup: 确保开发环境使用本地设置:

config.active_storage.service = :local

Adding all the required lines to the model: 将所有必需的行添加到模型:

class Article < ApplicationRecord
  has_one_attached :main_picture
  ... 
end

I did some fancy stuff, such as validation, custom variants, etc. - but for testing purposes and getting the basics working I commented out all that stuff. 我做了一些花哨的东西,例如验证,自定义变体等,但是出于测试目的和使基础知识正常工作,我注释掉了所有这些东西。

Update Controller to use permitted attributes specified in Pundit: 更新控制器以使用Pundit中指定的允许属性:

Controller 控制者

@article.update(permitted_attributes(@article))

Pundit 权威

class ArticlePolicy < ApplicationPolicy

  ...

  def permitted_attributes
    if !user.nil? && user.admin
      [:title, :content, :teaser, :slug, :published, :user_id, :main_picture]
    end
  end

  ...
end

Now the upload works like a breeze - to my local environment and I even tested uploading it to AWS, but let's stick to the local environment. 现在,上传变得轻而易举-到我的本地环境,我什至测试了将其上传到AWS的性能,但让我们坚持使用本地环境。 I can find the latest upload on the local environment in: 我可以在以下位置找到有关本地环境的最新上传:

/storage/N1/Ay/N1AyNaBeMNGhhmPSR69XwA9a

And that is the URL when I try to display the image in my view: 当我尝试在视图中显示图像时,这就是URL:

http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBQQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fd200a456532a80dfb122b2bdd9a53181b2a62aa/428KB.jpg

It appears like this in active_storage_blob table: 在active_storage_blob表中看起来像这样:

id,key,filename,content_type,metadata,byte_size,checksum,created_at
"55","N1AyNaBeMNGhhmPSR69XwA9a","428KB.jpg","image/jpeg","{""identified"":true,""width"":1920,""height"":1080,""analyzed"":true}","428299","48c8G3xQj5ENGgqqM08seQ==","2018-07-24 15:21:11"

Here is the various ways I tried to display the image: 这是我尝试显示图像的各种方法:

= image_tag @article.main_picture
= image_tag url_for(@article.main_picture)
= image_tag @article.main_picture.variant(resize_to_fit: [100, 100]

None of these options displays the image that was successfully uploaded and stored in the DB. 这些选项都不显示成功上载并存储在数据库中的图像。 All I get is the image placeholder. 我所得到的只是图像占位符。

For the latest to work (but didn't) I added the following to the GEMFILE (as per guide ): 对于最新的工作(但没有),我在GEMFILE中添加了以下内容(按照指南 ):

gem 'image_processing', '~> 1.2'

There is a similar threat complaining about this - using Rails 5.1 and adding active_storage in the Gemfile - but there is no real answer. 对此有类似的威胁抱怨-使用Rails 5.1并在Gemfile中添加active_storage-但没有真正的答案。 As suggested I tried adding: 根据建议,我尝试添加:

config/application.rb config / application.rb

require 'active_storage/engine'

Didn't help displaying the image :( 没有帮助显示图像:(

--- UPDATE: The logs as requested when accessing the URL --- ---更新:访问URL时请求的日志-

Started GET "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBQQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fd200a456532a80dfb122b2bdd9a53181b2a62aa/428KB.jpg" for 127.0.0.1 at 2018-07-25 12:41:36 +0100
Processing by ActiveStorage::BlobsController#show as JPEG
  Parameters: {"signed_id"=>"eyjfcmfpbhmionsibwvzc2fnzsi6ikjbahbqqt09iiwizxhwijpudwxslcjwdxiioijibg9ix2lkin19--fd200a456532a80dfb122b2bdd9a53181b2a62aa", "filename"=>"428kb"}
** [Localeapp] 1532518896 - Handling translation updates
** [Localeapp] 1532518896 - polling
** [Localeapp] API CALL: get https://api.localeapp.com/v1/projects/qXa7rByH1jQ9cNrU8t46zQkk8rkq3fMka13EACmQkXZ5FFTuUn/translations.yml?updated_at=1532518849
** [Localeapp] ATTEMPT 1
** [Localeapp] RESPONSE: 200
** [Localeapp] CALLING SUCCESS HANDLER: handle_success
** [Localeapp] 1532518897 - poll success
** [Localeapp] 1532518897 - reloading I18n
Filter chain halted as :set_blob rendered or redirected
Completed 404 Not Found in 917ms (ActiveRecord: 0.0ms)


Started GET "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBQQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fd200a456532a80dfb122b2bdd9a53181b2a62aa/428KB.jpg?disposition=attachment" for 127.0.0.1 at 2018-07-25 12:41:39 +0100
Processing by ActiveStorage::BlobsController#show as JPEG
  Parameters: {"disposition"=>"attachment", "signed_id"=>"eyjfcmfpbhmionsibwvzc2fnzsi6ikjbahbqqt09iiwizxhwijpudwxslcjwdxiioijibg9ix2lkin19--fd200a456532a80dfb122b2bdd9a53181b2a62aa", "filename"=>"428kb"}
** [Localeapp] 1532518899 - Handling translation updates
** [Localeapp] 1532518899 - polling
** [Localeapp] API CALL: get https://api.localeapp.com/v1/projects/qXa7rByH1jQ9cNrU8t46zQkk8rkq3fMka13EACmQkXZ5FFTuUn/translations.yml?updated_at=1532518897
** [Localeapp] ATTEMPT 1
** [Localeapp] RESPONSE: 200
** [Localeapp] CALLING SUCCESS HANDLER: handle_success
** [Localeapp] 1532518900 - poll success
** [Localeapp] 1532518900 - reloading I18n
Filter chain halted as :set_blob rendered or redirected
Completed 404 Not Found in 837ms (ActiveRecord: 0.0ms)

NOTE: 注意:

I added an image download button to generate the log: 我添加了一个图像下载按钮来生成日志:

= link_to "Download", rails_blob_path(@article.main_picture, disposition: "attachment")

Your signed_id seems invalid, it is why you got a 'Filter chain halted as :set_blob rendered or redirected'. 您的signed_id似乎无效,这就是为什么您使“过滤器链因:set_blob呈现或重定向而暂停”的原因。

Adding to the response: 添加到响应:

The problem was caused by case-sensitive URLs. 该问题是由区分大小写的URL引起的。 The GEM "route_downcaser" was installed. 已安装GEM“ route_downcaser”。 Upon removing this GEM everything worked as expected. 删除此GEM后,一切正常。

Also, "route_downcaser" offers the option of excluded_patterns. 另外,“ route_downcaser”提供了exclude_patterns选项。 So you can use the GEM together with active_storage but make sure you add the following to the initializer: 因此,您可以将GEM与active_storage一起使用,但请确保将以下内容添加到初始化程序中:

# config/initializers/route_downcaser.rb

RouteDowncaser.configuration do |config|
  config.redirect = true

  config.exclude_patterns = [
    /assets\//i,
    /fonts\//i,
    /active_storage\//i
  ]
end

More configuration options can be found here :) 更多配置选项可以在这里找到:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM