首页 > 解决方案 > Rails 5:活动存储链接添加 ?local=en

问题描述

我有一个产品模型,它通过 Active Storage 包含许多图像

class Product < ApplicationRecord
 has_many_attached :images
end

我的产品/展示视图将第一张图片显示为指向显示所有其他图片的模式的链接:

- if @product.images.present?
  = link_to image_tag(@product.images.first, class: "img-fluid"), "#", data: {toggle: "modal", target: "#images"}
  / or
  = link_to image_tag(url_for(@product.images.first)), "#", class: "img-fluid", data: {toggle: "modal", target: "#images"}


#images.modal
  .modal-dialog
    .modal-content    
      .modal-body
        .carousel-inner
          .carousel-item.active
            = image_tag = @product.images.first, class: 'd-block w-100'

          - @product.images.drop(1).each do |image|
            .carousel-item
              = image_tag image, class: 'd-block w-100'

      a.carousel-control-prev data-slide="prev" href="#image_controls" role="button" 
        span.carousel-control-prev-icon
      a.carousel-control-next data-slide="next" href="#image_controls" role="button" 
        span.carousel-control-next-icon

几分钟后效果很好。但不知何故,?locale=en几分钟后会从 Active Storage 添加到图像链接,因此图像不再显示。

<img src="/rails/active_storage/blobs/longstring/image_file.jpg">

变成

<img src="/rails/active_storage/blobs/longstring/image_file.jpg?locale=en">

我的路线文件:

scope "(:locale)", locale: /en|ja/ do
  resources :products

如何防止 Rails 添加locale=en到图像链接?

标签: ruby-on-railsrails-activestorageruby-on-rails-5.2

解决方案


事实证明问题不在于语言环境,而是通过以下方式解决了其他问题:https ://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond


推荐阅读