2013-06-20 18 views
3

使用Rails 3.2.13和2.0.2瘋狂
我遇到類似的問題,如Rails mountable engine under a dynamic scope用於安裝Rails的引擎設置default_url_options

我的路線:

scope ':locale', locale: /en|jp/ do 
    mount Spree::Core::Engine, at: '/store' 
    root to: 'home#index' 
end 

我要輸出鏈接改變的區域設置:

<%= 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 

它設置在店內現場PARAMS兩次,而不是合併的他們:

http://localhost:3000/en/store/products/bag?locale=en 
+0

你任何運氣來解決問題了嗎? 原因我目前遇到同樣的問題。 在此先感謝 – raskhadafi

+0

號_Please upvote for visibility_。 – NARKOZ

+0

我希望它會有所幫助! ;-) – raskhadafi

回答

0

面對完全一樣的問題,我已經找到了一個解決方案...

這裏是我的application_controller-文件(我的引擎從該文件(這是主要的應用程序的ApplicationController繼承,所以我不t代碼複製)

#!/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代碼在受保護的部分之外。它必須是公開的。

找到了解決這裏的竅門: default_url_options and rails 3

希望它可以幫助

問候

菲利普