Rails添加了可选的路线范围

时间:2018-06-28 11:42:34

标签: ruby-on-rails url-routing

我正在尝试添加可选的路由作用域或名称空间,但是在使其工作时遇到了一些麻烦。

我想像这样匹配网址:

https://www.mydoamin.com/en/cars/Audi
# Should be matched with Parameters: {"locale"=>"en", "brand"=>"Audi"}

https://www.mydoamin.com/en/cars/Audi/A4
# Should be matched with Parameters: {"locale"=>"en", "brand"=>"Audi", "car_model" => "A4"}

https://www.mydoamin.com/en/cars/in/Berlin
# Should be matched with Parameters: {"locale"=>"en", "region"=>"Berlin"}

https://www.mydoamin.com/en/cars/in/Berlin/Audi
# Should be matched with Parameters: {"locale"=>"en", "region"=>"Berlin", "brand"=>"Audi"}

https://www.mydoamin.com/en/cars/in/Berlin/Audi/A4
# Should be matched with Parameters: {"locale"=>"en", "region"=>"Berlin", "brand"=>"Audi", "car_model" => "A4"}

我当前的尝试如下:

scope ':locale' do
  scope 'cars' do
    scope '/in' do
      get ":region/(:brand)/(:car_model)" => 'cars_search#search', :as => :cars_search
    end

    get "(:brand)/(:car_model)" => 'cars_search#search', :as => :cars_search
  end
end

但是,如果没有in位置范围,例如https://www.mydoamin.com/en/cars/Audi/A4

,这些网址将不适用于那些网址

0 个答案:

没有答案