问题描述
使用 rails 3.2.13 和 spree 2.0.2
我遇到了与动态范围下的Rails可安装引擎
Using rails 3.2.13 and spree 2.0.2
I've encountered the similar problem as in Rails mountable engine under a dynamic scope
我的路线:
scope ':locale', locale: /en|jp/ do
mount Spree::Core::Engine, at: '/store'
root to: 'home#index'
end
我想输出链接以更改语言环境:
I want to output link to change locale:
<%= link_to 'JP', url_for(locale: :jp) %>
但是这个输出:
<a href="/en/store/?locale=jp">JP</a>
而不是预期:
<a href="/jp/store">JP</a>
-- 编辑 --
当我放入 ApplicationController
时:
def default_url_options(options={})
{ locale: I18n.locale }
end
它在 store 中设置 locale params 两次而不是合并它们:
it sets locale params in store twice instead of merging them:
http://localhost:3000/en/store/products/bag?locale=en
推荐答案
面临完全相同的问题,我已经找到了解决方案...
Faced exactly the same problem and I have found a solution for that...
这是我的 application_controller-File(我的引擎继承自这个文件(它是 Main Apps ApplicationController,所以我没有代码重复)
Here is my application_controller-File (my engines inherit from this file (which is the Main Apps ApplicationController, so that I don't have code-duplication)
#!/bin/env ruby
# encoding: utf-8
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :set_locale_from_params
def url_options
{ 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
end
请注意,url_options-code 在受保护部分之外.它必须是公开的.
Note, that the url_options-code is outside the protected part. It has to be public.
在这里找到解决方案的技巧:default_url_options 和 rails 3
Found the tipps for the solution here:default_url_options and rails 3
希望能帮到你
问候
菲利普
这篇关于为安装的 Rails 引擎设置 default_url_options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!