2012-04-25 62 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