本文介绍了从 Active Storage 更改默认 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以更改从活动存储创建的默认永久"url 以重定向到 S3.类似于 rails/active_storage/representations/.我不喜欢 url 中的框架名称.

Can we change the default 'permanent' url create from active storage to redirect to S3. Is something like rails/active_storage/representations/. I don't like the framework name in the url.

谢谢

推荐答案

更新:最近,在 Rails 6 中增加了路由前缀的配置:https://guides.rubyonrails.org/6_0_release_notes.html#active-storage-notable-changes

UPDATE: Recently, there was an addition which makes the route prefix configurable in Rails 6: https://guides.rubyonrails.org/6_0_release_notes.html#active-storage-notable-changes

这只是配置问题:

Rails.application.configure do
  config.active_storage.routes_prefix = '/whereever'
end

不幸的是,该 url 定义在 ActiveStorage routes.rb 没有容易改变的方法:


Unfortunately, the url is defined in ActiveStorage routes.rb without easy means to change:

get "/rails/active_storage/blobs/:signed_id/*filename" => 
     "active_storage/blobs#show", as: :rails_service_blob
get "/rails/active_storage/representations/:signed_blob_id/:variation_key/*filename" => 
     "active_storage/representations#show", as: :rails_blob_representation

我能想到的一个解决方案起点是另外定义您自己的路线并覆盖rails_blob_representation_path"或类似的

One solution starting point I can think of is defining your own Routes in addition and overriding the "rails_blob_representation_path" or similar

get "/my_uploads/:signed_blob_id/:variation_key/*filename" => 
  "active_storage/representations#show", as: :my_upload

然后覆盖帮助器文件中的路径并将帮助器包含到命名帮助器中:

and then overriding the path in a helper file and include the helper into the Named Helpers:

如何覆盖 rails 中的路由路径助手?

module CustomUrlHelper
  def rails_blob_representation(*args)
    my_upload(*args)
  end
end

# initializer etc.
Rails.application.routes.named_routes.url_helpers_module.send(:include, CustomUrlHelper)

该解决方案可能需要一些调整,但我没有对其进行测试.

The solution might need some adjustments though, I didn't tested it.

这篇关于从 Active Storage 更改默认 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:27