从Active Storage更改默认URL

时间:2018-05-02 06:23:52

标签: ruby-on-rails rails-activestorage

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

由于

1 个答案:

答案 0 :(得分:3)

<强>更新: 最近,有一个添加使路由前缀可配置:https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca

现在只在master分支中,但应该整合到〜&gt; 5.2.2 和更高。

然后,这只是配置问题:

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

不幸的是,网址是在ActiveStorage routes.rb中定义的,没有简单的方法可以更改:

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”或类似的

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

然后覆盖帮助文件中的路径并将帮助程序包含在命名助手中:

How to override routes path helper in 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)

解决方案可能需要一些调整,但我没有测试过。