为已安装的Rails引擎设置default_url_options
使用rails 3.2.13和spree 2.0.2
我在动态范围内遇到了与Rails可安装引擎类似的问题
我的路线:
scope ':locale', locale: /en|jp/ do mount Spree::Core::Engine, at: '/store' root to: 'home#index' end
我想输出更改区域设置的链接:
但这输出:
JP
而不是预期的:
JP
– 编辑 –
当我把它放到ApplicationController
:
def default_url_options(options={}) { locale: I18n.locale } end
它在商店中设置语言环境参数两次而不是合并它们:
http://localhost:3000/en/store/products/bag?locale=en
面对完全相同的问题,我找到了解决方案……
这是我的application_controller-File(我的引擎inheritance自这个文件(主应用程序ApplicationController,因此我没有代码重复)
#!/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代码位于受保护部分之外。 它必须是公开的。
在这里找到解决方案的tipps: default_url_options和rails 3
希望能帮助到你
问候
菲利普