【问题标题】:How can I make ActiveStorage work within my locales?如何使 ActiveStorage 在我的语言环境中工作?
【发布时间】:2018-06-30 05:35:54
【问题描述】:

如何使 Activestorage 在我的语言环境中工作,例如 www.localhost:3000/es/

我已经启动并运行了 activestorage。图像已保存并可在视图中查看。

然后,我使用以下代码将我的语言环境添加到我的路线中:

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do

    resources :posts
    root to: 'pages#index'

get '/*locale/*path', to: redirect("/#{I18n.default_locale}/%{path}")
get '/*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| I18n.available_locales.none? { |locale| req.path.starts_with? locale.to_s } }

我的application_controller.rb也有一个之前的操作

before_action :set_locale

def set_locale
  I18n.locale = params[:locale] if params[:locale].present?
end

def self.default_url_options(options = {})
  { locale: I18n.locale }
end

如果我删除上面的代码,则会显示图像。如何使 image_tags 与我的国际化一起使用?

【问题讨论】:

    标签: ruby-on-rails rails-i18n rails-activestorage


    【解决方案1】:

    我不得不改变我的路线。我添加了一个 LocaleConstraint。 以下是适用于 ActiveStorage 的路由:

    class LocaleConstraint
      def self.matches?(request)
        (I18n.available_locales.none? { |locale| request.path.starts_with? locale.to_s }) && (!request.path.include? 'rails/active_storage/blobs')
      end
    end
    
    Rails.application.routes.draw do
    
      scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    
        devise_for :users, controllers: { registrations: 'registrations' }
    
        resources :posts
    
        get '/*locale/*path', to: redirect("/#{I18n.default_locale}/%{path}")
        get '/*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: LocaleConstraint
    
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 2012-07-11
      相关资源
      最近更新 更多