gpt4 book ai didi

Ruby Rails 3.2 语​​言环境在 URL 中带有 folder_name 路径

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:39 24 4
gpt4 key购买 nike

我有三个 Ruby/Rails 小程序在以下设置下运行:

services.mydomain.com/applet1
services.mydomain.com/applet2
services.mydomain.com/applet3

这些是通过 Phusion Passenger 提供的,App1 和 App2 完全按预期工作。

问题出在 App3,它使用嵌入在 URL 中的 I18n Locales:

services.mydomain.com/applet3/en/
services.mydomain.com/applet3/fr/
services.mydomain.com/applet3/es/

问题似乎是 link_to URL 似乎不包含“applet3”。所以代替:

services.mydomain.com/applet3/en/signin

我得到:

services.mydomain.com/en/signin

...这是不可路由的。

我的 routes.rb 看起来像:

Rails.application.routes.draw do
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
resources :users
resources :sessions, only: [:new, :create, :destroy]

match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete

match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
root to: "static_pages#home"
end # scope locale

end # Rails.application.routes.draw do

... 在 app/controllers/application_controller.rb 我有:

  before_filter :set_locale_from_params

def url_options
{ only_path: true, locale: I18n.locale }
end

protected
def set_locale_from_params
if params[:locale]
if I18n.available_locales.include?(params[:locale].to_sym)
I18n.locale = params[:locale]
else
flash.now[:notice] = 'Translation not available'
logger.error flash.now[:notice]
end
end
end

... 在 config/environments/production.rb 我有:

Rails.application.routes.default_url_options[:host] = 'http://services.mydomain.com/applet3/'

EDIT1:添加示例 link_to 语句

<%= link_to(t(".#{language}"), root_path(locale: language)) %>

/编辑1

如果我将 App3 移动到更“正常”的排列,其中 URL 路径中没有“folder_name”,一切正常。因此,我缺少有关如何设置范围或 URL 选项的简单内容。

在此先感谢您的帮助和建议!

最佳答案

如何在你的路由中使用约束来定义你的子模块路由?

Rails.application.routes.draw do
constraints(:host) do
scope :path => ":locale" , locale: /#{I18n.available_locales.join("|")}/ do
resources :users
resources :sessions, only: [:new, :create, :destroy]

match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete

match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
root to: "static_pages#home"
end # scope locale
end # constraints

end # Rails.application.routes.draw do

关于Ruby Rails 3.2 语​​言环境在 URL 中带有 folder_name 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18643759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com