url_for如何检查来自routes.rb的约束

时间:2012-04-25 15:57:11

标签: ruby-on-rails routes

我尝试在rails应用程序上定义一些翻译路径。路径更改与子域定义。所以我想要这个结果:

  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

但是它始终是定义它使用的第一条路线。第二个永远不会定义。

我该怎么做?这是一个铁轨错误吗?

1 个答案:

答案 0 :(得分:0)

不确定它是否是一个rails bug,但看看https://github.com/francesc/rails-translate-routes它运作良好并可能有助于实现这一点。