Rails 5.2(升级)- Active Storage 不显示图像
Rails 5.2 (Upgrade) - Active Storage does not display image
所以我已经从 rails 5.1 升级到 5.2
rails app:update
一切正常,我立即设置了活动存储配置以用于 Web 应用程序中的新部分。
我创建了迁移:
rails active_storage:install
rake db:migrate
我配置了 conf/storage.yml - 使用 AWS S3 的生产:
测试:
服务:磁盘
根:<%= Rails.root.join("tmp/storage") %>
本地:
服务:磁盘
根:<%= Rails.root.join("storage") %>
亚马逊:
服务:S3
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
区域:eu-west-2
桶:随便
将所需的 GEM 添加到 GEMFILE:
gem 'aws-sdk-s3'
确保开发环境使用本地设置:
config.active_storage.service = :local
将所有必需的行添加到模型中:
class Article < ApplicationRecord
has_one_attached :main_picture
...
end
我做了一些花哨的事情,例如验证、自定义变体等 - 但为了测试目的和让基础工作发挥作用,我注释掉了所有这些东西。
更新控制器以使用 Pundit 中指定的允许属性:
控制器
@article.update(permitted_attributes(@article))
专家
class ArticlePolicy < ApplicationPolicy
...
def permitted_attributes
if !user.nil? && user.admin
[:title, :content, :teaser, :slug, :published, :user_id, :main_picture]
end
end
...
end
现在上传工作轻而易举 - 到我的本地环境,我什至测试了将它上传到 AWS,但让我们坚持使用本地环境。我可以在本地环境找到最新上传的:
/storage/N1/Ay/N1AyNaBeMNGhhmPSR69XwA9a
这就是 URL 当我尝试在我的视图中显示图像时:
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBQQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fd200a456532a80dfb122b2bdd9a53181b2a62aa/428KB.jpg
在active_storage_blobtable中是这样的:
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"
以下是我尝试显示图像的各种方式:
= image_tag @article.main_picture
= image_tag url_for(@article.main_picture)
= image_tag @article.main_picture.variant(resize_to_fit: [100, 100]
None 这些选项显示已成功上传并存储在数据库中的图像。我得到的只是图像占位符。
为了最新的工作(但没有),我将以下内容添加到 GEMFILE(根据 guide):
gem 'image_processing', '~> 1.2'
有一个 similar threat 抱怨这个 - 使用 Rails 5.1 并在 Gemfile 中添加 active_storage - 但没有真正的答案。按照建议,我尝试添加:
config/application.rb
require 'active_storage/engine'
没有帮助显示图像:(
--- 更新:访问 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)
注意:
我添加了一个图像下载按钮来生成日志:
= link_to "Download", rails_blob_path(@article.main_picture, disposition: "attachment")
您的 signed_id 似乎无效,这就是您获得 'Filter chain halted as :set_blob rendered or redirected' 的原因。
添加到响应中:
问题是由区分大小写的 URL 引起的。 GEM "route_downcaser" 已安装。删除此 GEM 后,一切都按预期工作。
此外,"route_downcaser" 提供了 excluded_patterns 的选项。因此,您可以将 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
可以找到更多配置选项here :)
所以我已经从 rails 5.1 升级到 5.2
rails app:update
一切正常,我立即设置了活动存储配置以用于 Web 应用程序中的新部分。
我创建了迁移:
rails active_storage:install
rake db:migrate
我配置了 conf/storage.yml - 使用 AWS S3 的生产:
测试: 服务:磁盘 根:<%= Rails.root.join("tmp/storage") %>
本地: 服务:磁盘 根:<%= Rails.root.join("storage") %>
亚马逊: 服务:S3 access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 区域:eu-west-2 桶:随便
将所需的 GEM 添加到 GEMFILE:
gem 'aws-sdk-s3'
确保开发环境使用本地设置:
config.active_storage.service = :local
将所有必需的行添加到模型中:
class Article < ApplicationRecord
has_one_attached :main_picture
...
end
我做了一些花哨的事情,例如验证、自定义变体等 - 但为了测试目的和让基础工作发挥作用,我注释掉了所有这些东西。
更新控制器以使用 Pundit 中指定的允许属性:
控制器
@article.update(permitted_attributes(@article))
专家
class ArticlePolicy < ApplicationPolicy
...
def permitted_attributes
if !user.nil? && user.admin
[:title, :content, :teaser, :slug, :published, :user_id, :main_picture]
end
end
...
end
现在上传工作轻而易举 - 到我的本地环境,我什至测试了将它上传到 AWS,但让我们坚持使用本地环境。我可以在本地环境找到最新上传的:
/storage/N1/Ay/N1AyNaBeMNGhhmPSR69XwA9a
这就是 URL 当我尝试在我的视图中显示图像时:
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBQQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fd200a456532a80dfb122b2bdd9a53181b2a62aa/428KB.jpg
在active_storage_blobtable中是这样的:
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"
以下是我尝试显示图像的各种方式:
= image_tag @article.main_picture
= image_tag url_for(@article.main_picture)
= image_tag @article.main_picture.variant(resize_to_fit: [100, 100]
None 这些选项显示已成功上传并存储在数据库中的图像。我得到的只是图像占位符。
为了最新的工作(但没有),我将以下内容添加到 GEMFILE(根据 guide):
gem 'image_processing', '~> 1.2'
有一个 similar threat 抱怨这个 - 使用 Rails 5.1 并在 Gemfile 中添加 active_storage - 但没有真正的答案。按照建议,我尝试添加:
config/application.rb
require 'active_storage/engine'
没有帮助显示图像:(
--- 更新:访问 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)
注意:
我添加了一个图像下载按钮来生成日志:
= link_to "Download", rails_blob_path(@article.main_picture, disposition: "attachment")
您的 signed_id 似乎无效,这就是您获得 'Filter chain halted as :set_blob rendered or redirected' 的原因。
添加到响应中:
问题是由区分大小写的 URL 引起的。 GEM "route_downcaser" 已安装。删除此 GEM 后,一切都按预期工作。
此外,"route_downcaser" 提供了 excluded_patterns 的选项。因此,您可以将 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
可以找到更多配置选项here :)