2012-04-25 57 views
0

我嘗試在我的rails應用程序上定義一些轉換路線。子域的路由改變定義。所以我想要這個結果:url_for如何檢查我的routes.rb中的約束條件

describe "url_for" do 
    context 'with en' do 
     it 'brand translate' do 
     url_for(
      :controller => 'boats', 
      :action => 'index', 
      :subdomain => :en, 
      :brand => 'hello',                                       
      :only_path => true 
     ).should == '/yacht-charter/brand-hello' 
     end 
    end 

    context 'with fr' do 
     it 'brand translate' do 
     url_for(
      :controller => 'boats', 
      :action => 'index', 
      :subdomain => :fr, 
      :brand => 'hello', 
      :only_path => true 
     ).should == '/location-bateau/marque-hello' 
     end 
    end 
    end 

就像你可以看到兩個url_for params之間的唯一變化是子域。我嘗試:

constraints => :subdomain => :en do                             
    match '/yacht-charter/brand-:brand' => 'boats#index', :as => 'en_brand_search' 
    end 

    constraints :subdomain => :fr do 
    match '/location-bateau/marque-:brand' => 'boats#index', :as => 'fr_brand_search' 
    end 

但始終它是第一個定義它使用的路線。第二個是從來沒有定義。

我該怎麼做。這是一個軌道錯誤或不?

回答

0

不知道它是否是一個軌道錯誤,但看看https://github.com/francesc/rails-translate-routes它運作良好,可能有助於實現這一目標。

+0

我已經測試了這個寶石,它的工作正常,但不適用於像''/ yacht-charter/brand-:brand'這樣的網址,它只能與'/ yacht-charter/brand /:brand'這樣的網址一起使用。 – shingara 2012-04-25 19:09:10